nested loop only executing once: why is this?
Posted: Mon Nov 23, 2009 4:28 pm
Hey I tried using a standard approach to create a blog with earlier but am trying a different approach that seems to work better but I am still experiencing a few problems. This works by using a loop to display all of the blog topics. There is a nested loop inside that displays the comments for the topic. In other words for every run of the outer loop the topic is written and the nested loop is run to display the all of the comments. The problem is that all of the comments are written for the first post, but not for any of the other topics. I suspected that the issue was that the nested loop is only being run for the first iteration of the outer loop. I wanted to determine whether the problem was that the nested loop was not actually executing or if there was just some kind of problems with the conditionals inside the loop the second time around. So I tested this by adding some simple 'echo' statements written to be executed every time the loop is executed, and sure enough they only run the first time around. This seems to suggest that my theory is correct and that the problem is that the nested loop only executes once. I am trying to figure out why.
I am using the variable $i to help establish the number of times that the nested loop is supposed to execute. It is supposed to be set to 0 and incremented each time the loop is run and then stop when $i equals the number of comments associated with the given topic. At first the problem seemed to be that $i was not reset to 0 after the first run of the outer loop, so I tried simply using the statement $i=0 inside the outer loop and outside the nested loop but ended up getting an error message. Therefore just before the nested loop I tried the statement:
if ($i > $numcomments)
{
$i = 0;
}
Since the nested loop is supposed to run every time $i is less than the total number of comments I was hoping that this code would cause the nested loop to run every time the outer loop is run, but it is still only running the first time the outer loop is executed. I know it might make more sense to use a for loop instead of a while loop for the nested loop but I tried this and I still got the same results, which suggested to me that the problem might not be the conditionals that I am using in the while loop. However, I am totally lost at this point about what the problem is.
I am just trying to give feedback on what I have done and what I think the problems might be but I am not 100% sure about what the real issue is here. If you disagree with my theories and have any feedback on the problem, please let me know. Anything you can give me would be appreciated. Thanks, my code is listed below:
$query2 = 'select * from shtopics';
$result2 = $db->query($query2);
$numcomments = $result2->num_rows;
$query = 'select * from shposts';
$result = $db->query($query);
$numposts = $result->num_rows;
$j = 0;
$i = 0;
while ($j < $numposts)
{
$entry = $result->fetch_assoc();
$postid = $entry['postID'];
echo $postid;
$blogtext = $entry['Title'];
echo $blogtext;
if ($i > $numcomments)
{
$i = 0;
}
/* Of course $i is also = 0 the first time the loop is run because I set it to 0 just before the loop was run */
while ($i < $numcomments)
{
$centry = $result2->fetch_assoc();
if ($postid == $centry['postID'])
{
echo 'hi there';
$comment = $centry['comment'];
echo $comment;
$commentid = $centry['commentID'];
echo $commentid;
}
$i++;
}
$j++;
}
I am using the variable $i to help establish the number of times that the nested loop is supposed to execute. It is supposed to be set to 0 and incremented each time the loop is run and then stop when $i equals the number of comments associated with the given topic. At first the problem seemed to be that $i was not reset to 0 after the first run of the outer loop, so I tried simply using the statement $i=0 inside the outer loop and outside the nested loop but ended up getting an error message. Therefore just before the nested loop I tried the statement:
if ($i > $numcomments)
{
$i = 0;
}
Since the nested loop is supposed to run every time $i is less than the total number of comments I was hoping that this code would cause the nested loop to run every time the outer loop is run, but it is still only running the first time the outer loop is executed. I know it might make more sense to use a for loop instead of a while loop for the nested loop but I tried this and I still got the same results, which suggested to me that the problem might not be the conditionals that I am using in the while loop. However, I am totally lost at this point about what the problem is.
I am just trying to give feedback on what I have done and what I think the problems might be but I am not 100% sure about what the real issue is here. If you disagree with my theories and have any feedback on the problem, please let me know. Anything you can give me would be appreciated. Thanks, my code is listed below:
$query2 = 'select * from shtopics';
$result2 = $db->query($query2);
$numcomments = $result2->num_rows;
$query = 'select * from shposts';
$result = $db->query($query);
$numposts = $result->num_rows;
$j = 0;
$i = 0;
while ($j < $numposts)
{
$entry = $result->fetch_assoc();
$postid = $entry['postID'];
echo $postid;
$blogtext = $entry['Title'];
echo $blogtext;
if ($i > $numcomments)
{
$i = 0;
}
/* Of course $i is also = 0 the first time the loop is run because I set it to 0 just before the loop was run */
while ($i < $numcomments)
{
$centry = $result2->fetch_assoc();
if ($postid == $centry['postID'])
{
echo 'hi there';
$comment = $centry['comment'];
echo $comment;
$commentid = $centry['commentID'];
echo $commentid;
}
$i++;
}
$j++;
}