Page 1 of 1

looping twice function

Posted: Sun Apr 04, 2010 12:52 pm
by rafaell
I'm trying to write a code to create a menu from a database. The code is supposed to find the 'page_name' which is the menu name and then loop through all existing "sections" of that menu (appetizers, salad, soup...), then loop through each section to find the food items.

I guess what I envision in the end is I have a 'menu' function where I can put wherever I want that section to appear. ex: menu(dinner,salad) and the dinner salad will section appears.

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

Code: Select all

$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'];
};
};
};
};

Re: looping twice function

Posted: Sun Apr 04, 2010 1:07 pm
by Christopher
rafaell wrote:After a few days of going crazy over this, I think my code just stopped making sense.
That is an understatement. ;) You should get a PHP book or take a PHP class because you do not understand the basic syntax.

Code: Select all

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'];

    $menu_html = " /* CREATE HTML FOR THIS MENU ITEM HERE */ ";
    return $menu_html;
  }
}

$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'];
  $html = '';

  $html .= " /* MENU HTML BEFORE MENU ITEMS */ ";
  foreach ($section_name as $section_name)
  {
    $html .= create_menu($menu_name, $section_name);
  }
}
$html .= " /* MENU HTML AFTER MENU ITEMS */ ";

echo $html;

Re: looping twice function

Posted: Sun Apr 04, 2010 6:42 pm
by rafaell
Christopher wrote:
rafaell wrote:After a few days of going crazy over this, I think my code just stopped making sense.
That is an understatement. ;) You should get a PHP book or take a PHP class because you do not understand the basic syntax.
Yes! this worked. Well, the top half did everything. I don't think I'll need the second half for the before/after. Wherever I insert the create_menu function, it does the job.

I am definitely still learning, and have a few books, but I am mostly learning by trial and error. Sometimes things seem more complicated in my head then it actually has to be.

Thanks!

Re: looping twice function

Posted: Sun Apr 04, 2010 6:43 pm
by rafaell
Christopher wrote:
rafaell wrote:After a few days of going crazy over this, I think my code just stopped making sense.
That is an understatement. ;) You should get a PHP book or take a PHP class because you do not understand the basic syntax.
Yes! this worked. Well, the top half did everything. I don't think I'll need the second half for the before/after. Wherever I insert the create_menu function, it does the job.

I am definitely still learning, and have a few books, but I am mostly learning by trial and error. Sometimes things seem more complicated in my head then it actually has to be.

Thanks!

Re: looping twice function

Posted: Sun Apr 04, 2010 7:06 pm
by samwho
If I were you, I'd try and learn to write good, clean code at the same time as learning the language itself :)

Take a look at the PEAR coding standards: http://pear.php.net/manual/en/standards.php