Jump to content

Nested While loop issue.


YourNameHere

Recommended Posts

I am building a menu for a restaurant website. in the admin section you can add categories and then the menu items. so to print this out on the actual menu page, I have desided to do a nested while loop. First it gets each category and echos the name of the category. Then it gets all the items in that catagory and echos those below that category title.

 

The title prints but the menu items dont.

 

// get categories
    $cat_sql = "select * from catagories";
    $cat_results = mysql_query($cat_sql);
    $cat_count = mysql_num_rows($cat_results);

while ($i < $cat_count)
                                {
                                    // For every Category, print the cat name
                                    $category = mysql_result($cat_results, $i, cat_name);
                                    
                                    echo $category;
                                    
                                    $sql1="select * from menu where cat = '$catagory'";
                                    $results = mysql_query($sql1);
                                    $sql1_count = mysql_num_rows($results);
                                    
// echo out all the item in this cat
                                    while ($c < $sql1_count)
                                    {    
                                    $item_name = mysql_result($results, $c, title);
                                    $descr = mysql_result($results, $c, Description);
                                    $price =  mysql_result($results, $c, price);
                                    
                                    
                                        echo $item_name.' - '.$price.' - '.$descr;
                                        $c++;
                                    } 
                                    $i++;
                                }
                                ?>

 

I stripped out any other html that is echoed here for readability.

Everything I have read says that other peoples issue is that the loop returns false as it already looped over the result set but I have two distinct result sets.

Link to comment
https://forums.phpfreaks.com/topic/195052-nested-while-loop-issue/
Share on other sites

when you use mysql_result you need to put the field name in quotes.

$category = mysql_result($cat_results, $i, "cat_name");

 

that is likely your problem.

 

Is the just a best practice because I have always not put it in quote and it's always worked with the mysql_result function.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.