YourNameHere Posted March 12, 2010 Share Posted March 12, 2010 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 More sharing options...
ZombieBento Posted March 12, 2010 Share Posted March 12, 2010 Check your spelling. You have category and catagory. That might be the problem. Link to comment https://forums.phpfreaks.com/topic/195052-nested-while-loop-issue/#findComment-1025339 Share on other sites More sharing options...
schilly Posted March 12, 2010 Share Posted March 12, 2010 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. Link to comment https://forums.phpfreaks.com/topic/195052-nested-while-loop-issue/#findComment-1025340 Share on other sites More sharing options...
YourNameHere Posted March 12, 2010 Author Share Posted March 12, 2010 Check your spelling. You have category and catagory. That might be the problem. I'm an idiot, thanks. Link to comment https://forums.phpfreaks.com/topic/195052-nested-while-loop-issue/#findComment-1025344 Share on other sites More sharing options...
YourNameHere Posted March 12, 2010 Author Share Posted March 12, 2010 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 https://forums.phpfreaks.com/topic/195052-nested-while-loop-issue/#findComment-1025351 Share on other sites More sharing options...
XeNoMoRpH1030 Posted March 12, 2010 Share Posted March 12, 2010 PHP is pretty smart. Depending on your configuration, you can omit the quotes and it will work. Doesn't mean it is right though. Link to comment https://forums.phpfreaks.com/topic/195052-nested-while-loop-issue/#findComment-1025358 Share on other sites More sharing options...
YourNameHere Posted March 13, 2010 Author Share Posted March 13, 2010 PHP is pretty smart. Depending on your configuration, you can omit the quotes and it will work. Doesn't mean it is right though. Yeah, so it's best practice. I looked in the docs and it doesn't mention quotes, so that's why I was confused I suppose. Link to comment https://forums.phpfreaks.com/topic/195052-nested-while-loop-issue/#findComment-1025515 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.