View Full Version : Problems With Nested "While" Loops - PHP


onigiri
12-16-2006, 06:35 AM
Erm, yeah, the title is pretty self-explanatory. :P I'm having problems with my nested "while" loops, the problem being that the second one (which is nested in the first one) won't execute.


<?php

//connect to the database
require("includes/config.php");

//select all the categories in the database
$get_cat = "SELECT cat_id, cat_name FROM categories";
$get_cat_res = mysql_query($get_cat) or die(mysql_error());

//get the number of categories returned (number of rows)
if(mysql_num_rows($get_cat_res) < 1) {//no categories
$display_block = "No categories";
} else {

//"while" loop for the categories
while($cat_info = mysql_fetch_array($get_cat_res)) {
$cat_id = $cat_info['category_id'];
$cat_name = $cat_info['category_name'];

//create display
$display_block = "
<div class='border'>
<table width='100%' border='0' cellspacing='1' cellpadding='4'>
<tr>
<td class='row1' align='left'>$cat_name</td>
</tr>
";

//get all the forums that belong to that certain category
$get_forums = "SELECT forum_id, forum_name, forum_parent, forum_description FROM forums WHERE forum_parent = '$cat_name'";
$get_forums_res = mysql_query($get_forums) or die(mysql_error());

//"while" loop for the forums

//create the display string
$display_block .= "
<tr class='row2'>
<th>Marker</th>
<th>Forum Name</th>
<th>Number of Posts</th>
<th>Last Post</th>
</tr>";

while($forum_info = mysql_fetch_array($get_forums_res)) {
$forum_id = $forum_info['forum_id'];
$forum_name = $forum_info['forum_name'];
$forum_parent = $forum_info['forum_parent'];
$forum_description = $forum_info['forum_description'];

//get number of posts
$get_num_posts = "SELECT COUNT($post_id) FROM forum_posts WHERE forum_id = $forum_id";
$get_num_posts_res = mysql_query($get_num_posts) or die(mysql_error());
$posts_info = mysql_fetch_array($get_num_posts);
$num_posts = $posts_info['count(post_id)'];

if($num_posts < 1) { // no posts
$display_block .= "
<tr>
<td class='row3'><img src='images/forummarker.jpg'></td>
<td class='row3'><a href='topiclist.php?forum_id=$forum_id'>$forum_name</a></td>
<td class='row4'>0</td>
<td class='row4'>Not applicable</td>
</tr>
<p></p>
<p></p>
";
} else {

//get the information about the last post
$get_last_post = "SELECT post_owner, post_create_time FROM forum_posts WHERE forum_id = $forum_id ORDER BY post_id DESC LIMIT 1";
$get_last_post_res = mysql_query($get_last_post) or die(mysql_error());
$last_post_info = mysql_fetch_array($get_last_post_res);
$last_post_owner = $last_post_info['post_owner'];
$last_post_create_time = $last_post_info['post_create_time'];

//add to display
$display_block .= "
<tr>
<td class='row3'><img src='images/forummarker.jpg'></td>
<td class='row3'><a href='topiclist.php?forum_id=$forum_id'>$forum_name</a></td>
<td class='row4'>$num_posts</td>
<td class='row4'>$last_post_owner<br>$last_post_create_time</td>
</tr>
<p></p>
<p></p>";
}
}



}
}
//close up the table and div
$display_block .= "</table></div>";

?>

war59312
12-19-2006, 02:47 AM
I think you can now figure it out on your own... ;)<?php

if ( ) {

} else {

while ( ) :

while ( ) {

if ( ) {

} else {

}
}

endwhile;
}
?>

onigiri
12-19-2006, 04:59 AM
Thanks for your help, Mr. Rolison. :D I'm trying your solution now. *Crosses fingers*

onigiri
12-19-2006, 05:34 AM
Thanks for your help, but I think I found out either another problem or what the real problem is. See, this script is a simple message board script, and this bit of code here


$get_forums = "SELECT forum_id, forum_name, forum_description FROM forums WHERE forum_parent = '$cat_id'";

is supposed to select all the forums that belong to a certain category. The $cat_id variable is from this query:


$get_cat = "SELECT cat_id, cat_name FROM categories";

But whenever I do that, no forums show up on the index page, even though there are forums for that category. It works whenever I make the query WHERE forum_parent = '(id of specific forum)' instead of 'WHERE forum_parent = '$variable', however.

Here's the full code I'm using; sorry for the double post.


<?php

//connect to the database
require("includes/config.php");

//select all the categories in the database
$get_cat = "SELECT cat_id, cat_name FROM categories";
$get_cat_res = mysql_query($get_cat) or die(mysql_error());

if (mysql_num_rows($get_cat_res) < 1 ) { //no rows
$display_block = "No categories";
} else {

while ($cat_info = mysql_fetch_array($get_cat_res)) :
$cat_id = $cat_info['category_id'];
$cat_name = $cat_info['category_name'];

//create display
$display_block = "
<div class='border'>
<table width='100%' border='0' cellspacing='1' cellpadding='4'>
<tr>
<td class='row1' align='left'>$cat_name</td>
</tr>
";

//get all the forums that belong to that certain category
$get_forums = "SELECT forum_id, forum_name, forum_description FROM forums WHERE forum_parent = '$cat_id'";
$get_forums_res = mysql_query($get_forums) or die(mysql_error());

//"while" loop for the forums

//create the display string
$display_block .= "
<tr class='row2'>
<th>Marker</th>
<th>Forum Name</th>
<th>Number of Posts</th>
<th>Last Post</th>
</tr>";

while ($forum_info = mysql_fetch_array($get_forums_res)) {
$forum_id = $forum_info['forum_id'];
$forum_name = $forum_info['forum_name'];
$forum_parent = $forum_info['forum_parent'];
$forum_description = $forum_info['forum_description'];

//get number of posts
$get_num_posts = "SELECT post_id FROM forum_posts WHERE forum_id = '$forum_id'";
$get_num_posts_res = mysql_query($get_num_posts) or die(mysql_error());
$num_posts = mysql_num_rows($get_num_posts_res);

if ($num_posts < 1) {
$display_block .= "
<tr>
<td class='row3'><img src='images/forummarker.jpg'></td>
<td class='row3'><a href='topiclist.php?forum_id=$forum_id'>$forum_name</a></td>
<td class='row4'>0</td>
<td class='row4'>Not applicable</td>
</tr>
<p></p>
<p></p>
";

} else {
//get the information about the last post
$get_last_post = "SELECT post_owner, post_create_time FROM forum_posts WHERE forum_id = '$forum_id' ORDER BY post_id DESC LIMIT 1";
$get_last_post_res = mysql_query($get_last_post) or die(mysql_error());
$last_post_info = mysql_fetch_array($get_last_post_res);
$last_post_owner = $last_post_info['post_owner'];
$last_post_create_time = $last_post_info['post_create_time'];

//add to display
$display_block .= "
<tr>
<td class='row3'><img src='images/forummarker.jpg'></td>
<td class='row3'><a href='topiclist.php?forum_id=$forum_id'>$forum_name</a></td>
<td class='row4'>$num_posts</td>
<td class='row4'>$last_post_owner<br>$last_post_create_time</td>
</tr>
<p></p>
<p></p>";

}
}

endwhile;
//close up the table and div
$display_block .= "</table></div>";
}
?>