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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.