?
Solved

PHP While inside While problem

Posted on 2009-05-05
8
Medium Priority
?
264 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:DevinPitcher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 24311200
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

0
 

Author Comment

by:DevinPitcher
ID: 24312868
@Michael701

3 results, like I have in the database.
0
 
LVL 2

Expert Comment

by:-pio-
ID: 24313373
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
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

by:DevinPitcher
ID: 24313733
@-pio-

I will try that when I get home. That looks like it just may work.
0
 
LVL 19

Expert Comment

by:Michael701
ID: 24315714
Did you view (verify) that the generated html is valid? Can you post a sample of the generated html?
0
 

Author Comment

by:DevinPitcher
ID: 24318193
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

0
 

Accepted Solution

by:
ee_auto earned 0 total points
ID: 24599836
Question PAQ'd, 250 points refunded, and stored in the solution database.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question