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>";
?>
<?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>";
?>