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
...
}