Jump to content

Loopping twice


rafaell

Recommended Posts

I'm trying to write a code to create a menu from a database. The code is supposed to loop through all existing "sections" (appetizers, salad, soup...), then loop through each section to find the food items.

 

After a few days of going crazy over this, I think my code just stopped making sense.

 

$menu_info_tbl = mysql_query("SELECT * FROM menu_info WHERE page_name='$menu_name'");
while($row = mysql_fetch_array($menu_info_tbl))
{
$section_name = $row['section_name'];
$menu_name = $row['page_name_name'];
foreach
($section_name as $section_name and $menu_name as $menu_name)
{
function create_menu($menu_name,$section_name)
{
$menu_info_tbl = mysql_query("SELECT * FROM menu_info WHERE page_name='$menu_name' AND section_name='$section_name'");
while($row = mysql_fetch_array($menu_info_tbl))
{
$food_id = $row['food_id'];
$section_name = $row['section_name'];
$page_name = $row['page_name'];
$food_name = $row['food_name'];
$food_price = $row['food_price'];
$food_description = $row['food_description'];
};
};
};
};

Link to comment
https://forums.phpfreaks.com/topic/197472-loopping-twice/
Share on other sites

Not really sure what you were doing defining a function inside of a loop, that will cause a fatal error and is just bad practice.

 

Here is one way you could do it, basically generates an array you can loop through later to display your data.

 

<?php

$menu_info_tbl = mysql_query("SELECT food_id, section_name, page_name, food_name, food_price, food_description
						 FROM menu_info WHERE page_name='$menu_name' GROUP BY page_name, section_name");

$menu_array = array();
while($row = mysql_fetch_assoc($menu_info_tbl)) {
if (!in_array($row['page_name'], $menu_array)) 
	$menu_array[$row['page_name']] = array();

$menu_array[$row['page_name']][] = $row;
}

$display = "";
foreach ($menu_array as $page_name => $menu_item) {
$display .= 'Page: ' . $page_name . '<br />';
foreach ($menu_item as $item) {
	$display .= 'Item: ' . $item['food_id'] . '<br />Food Name:' . $item['food_name'] . '<br />Description: ' . $item['food_description'] . '<br /><br />';
}
$display .= '<br />';
}

echo 'Have a look at our menu!<br /><br />' . $display;
?>

Link to comment
https://forums.phpfreaks.com/topic/197472-loopping-twice/#findComment-1036463
Share on other sites

Not really sure what you were doing defining a function inside of a loop, that will cause a fatal error and is just bad practice.

 

I guess what I was trying to do was have a function where I can insert it where I want that section to appear.

For example, on the dinner page, have 4 functions which would generate a section for salads, soups, appetizers, and dessert. Of course with HMTL and image in between, so I just need to call the individual sections.

 

The function would be like menu(dinner, soups)

 

I did try the code you gave me but it seems like it only pulls the first entry on the table.

Link to comment
https://forums.phpfreaks.com/topic/197472-loopping-twice/#findComment-1036498
Share on other sites

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.