Link to home
Start Free TrialLog in
Avatar of DevinPitcher
DevinPitcher

asked on

PHP While inside While problem

I am trying to run a while script inside another while script, but it stops after one result.
What am I doing wrong?
<?php $cat_sql="SELECT * FROM cb_categories ORDER BY id ASC"; // Get all the categories and forums.
	$cat_result=mysql_query($cat_sql);
	while($cat_rows=mysql_fetch_array($cat_result)){ // Show next cateogry.
	?>
	<div class="index_cat_top"><font face="Comic Sans MS, cursive"><b><?php echo $cat_rows['name']; ?></b></font></div>
	<div class="index_cat_mid"><center><?php 
        $forum_sql="SELECT * FROM cb_forums WHERE category_id = $cat_rows[id] ORDER BY forum_id ASC";
        $forum_result=mysql_query($forum_sql);
        ?>
        <table width="820px" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">     
        <?php
        while($forum_rows=mysql_fetch_array($forum_result) or die(mysql_error())){ // Get all the forums in the category 
        ?>
        <tr>
        <td bgcolor="#FFFFFF"><? echo $forum_rows['forum_id']; ?></td>
        <td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $forum_rows['forum_id']; ?>"><? echo $forum_rows['name']; ?></a><BR></td>
        </tr>
        <?php } ?>
        </table></center>
    </div>
	<div class="index_cat_bot"></div>
    <br /><br />
<?php } ?>

Open in new window

Avatar of Michael701
Michael701
Flag of United States of America image

which loop is stopping?

I really don't see any syntax issues.

try this

        $cat_result=mysql_query($cat_sql);
echo "categories: ". mysql_num_rows($cat_result) ."<br />\n";

let's see how many records are getting returned.

btw: if you just use echo statments you won't have to be switching in and out of php and your code should look cleaner.

see attached
        while($cat_rows=mysql_fetch_array($cat_result)){ // Show next cateogry.
        echo "<div class='index_cat_top'><font face='Comic Sans MS, cursive'><b>".$cat_rows['name']. "</b></font></div>\n";
        echo "<div class='index_cat_mid'><center>\n"; 
        $forum_sql="SELECT * FROM cb_forums WHERE category_id = $cat_rows[id] ORDER BY forum_id ASC";
        $forum_result=mysql_query($forum_sql);
        echo "<table width='820px' border=0 align='center' cellpadding=3 cellspacing=1 bgcolor='#CCCCCC'>\n";
        while($forum_rows=mysql_fetch_array($forum_result) or die(mysql_error())){ // Get all the forums in the category 

Open in new window

Avatar of DevinPitcher
DevinPitcher

ASKER

@Michael701

3 results, like I have in the database.
Hi,
At first sight I see no problem with the code but

I would change this line:

$forum_sql="SELECT * FROM cb_forums WHERE category_id = $cat_rows[id] ORDER BY forum_id ASC";

to this:

$forum_sql="SELECT * FROM cb_forums WHERE category_id = ".$cat_rows[id]." ORDER BY forum_id ASC";

Regards,
Peter
@-pio-

I will try that when I get home. That looks like it just may work.
Did you view (verify) that the generated html is valid? Can you post a sample of the generated html?
Found the issue: Line 12 above.
the or die command had messed it up. Works great now.
while($forum_rows=mysql_fetch_array($forum_result) or die(mysql_error())){ // Get all the forums in the category

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ee_auto
ee_auto

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial