1

I have a standardized sequence of code which I used to display topic information on the home page and other pages on my site. I want to throw it in an include file so that I don't have to keep fixing multiple pages. The problem is sometimes this include occurs in a while statement which means there was a previous query supplying information for sorting purposes.

When the code is raw in the while statement, it works as it should and if there are multiple ids being served to the code from the previous query, it shows multiple results. However, if this code is in an include, I will only see one result. I assume because the include file only executes once. How would I serve an include file as the equivalent as my raw code?

Include

//outside query
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    $topic_id=htmlspecialchars( $row['topic_id'], ENT_NOQUOTES, 'UTF-8' );
    //code to display topics
    include('display_topics.php');  
}

Raw Code

//outside query
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    //code to display topics
    $sql = "SELECT * FROM topic WHERE id=?";
    $stmt = $conn->prepare($sql);
    $stmt->bindValue(1,topic_id, PDO::PARAM_INT);
    $result=$stmt->execute();
}

while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    //yada yada
    ...
}
3
  • 2
    Make it as a FUNCTION -- one of it's purposes is to implement a code snippet that can be called multiple times with (possibly) different arguments -- in other words "code reuse". Commented Jun 21, 2011 at 19:19
  • Would it be possible to restructure display_topics.php so it provides a function you call to perform its operations rather than having it just run? Commented Jun 21, 2011 at 19:20
  • Possible duplicate of PHP include file multiple times in one page Commented Oct 13, 2018 at 2:21

3 Answers 3

12

Don't do it this way.

It's much better to do one include, and to declare a function in it that does what you need:

function display_topics($topic_id)
 { 
   ....
 }

Call that function inside the loop, and pass all necessary data to it.

Sign up to request clarification or add additional context in comments.

9 Comments

Ya, this is the better way to go :)
Curses! Foiled again by a concise answer!
Wow you are getting mad votes. That is the fastest I have seen +10.
@Scarface yeah, and I don't entirely understand why! It's not that outstanding an answer really. Ah well. :)
It's not the size, it's how you use it haha. It got the job done.
|
1

Include the file as normal, but put the repeated code in a function. Then, where you wish to add the code simply call the function.

Comments

1

That word, I don't think it is for what you think it is for.

Generally, include's use should not be used that way. That's why God invented functions (or Ada Byron, if you're not theologically inclined).

Try this instead:

function execute_row($row, $conn)
{
    $topic_id=htmlspecialchars( $row['topic_id'], ENT_NOQUOTES, 'UTF-8' );
    $sql = "SELECT * FROM topic WHERE id=?";
    $stmt = $conn->prepare($sql);
    $stmt->bindValue(1,topic_id, PDO::PARAM_INT);
    return $stmt->execute();
}

Then, in your while statement:

while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    $result = execute_row($row, $conn);
}

TADA! And the best part is, it makes your code smaller, easier to read, and faster to parse.

3 Comments

I agree functions should be used, but within an include in this case. This function is reoccurring on about 7 different pages so the reason I even thought of include was so I did not have to go around fixing all these pages, I could just fix one.
@Scarface you would keep the function in one location location. Or if they're 7 similar but different ones, the next step would be to build a function so abstract that it can serve all 7 purposes.
Are you referring to OOP type techniques?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.