Writing out a menu with a php loop?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Writing out a menu with a php loop?

Post by mischievous »

Hey I am trying to setup where the menus for my site are stored in a mysql database for backend editability.

I have a parent menu table(menus) with id, name, and alias.

I have a child menu table(menu_items) with id, menu_id, name, alias, href, parent_id, and order

I need to have a script run through the table and layout a un-ordered list
ie:

Code: Select all

<ul>
  <li>Menu
     <ul>
        <li>Sub-Menu</li>
     </ul>
  </li>
</ul>
How would i go about doing this?

Thanks,
Anthony
Last edited by Benjamin on Tue May 26, 2009 4:26 pm, edited 1 time in total.
Reason: Changed code type from text to html.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Writing out a menu with a php loop?

Post by mikemike »

A simple database query and while loops would do it, you'd need to nest a loop inside a loop inorder to do your menu and sub-menu.

Once you understand while loops it should be pretty easy. Here is a tutorial.

Good luck
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: Writing out a menu with a php loop?

Post by mischievous »

Thats what I am talkin about! Haha... thats great information McInfo!!! I really really appreciate the help!

Thanks for your help as well mikemike!

Will test this tomorrow at work!

Thanks,
Anthony :mrgreen:
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Writing out a menu with a php loop?

Post by mikemike »

McInfo's solution is probably faster than nested loops but if in the future you decided to add a sub-menu to your sub-menus it is likely to cause you problems. Just something to bare in mind!
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: Writing out a menu with a php loop?

Post by mischievous »

Ok guys... decided that I needed some deeper hierarchy, this is the code im running with currently and its displaying the menu like its suppost to however, its repeating the same menu up to 3 times...
The reason is because im running the initial query that runs through all items and then inside the child loops its running through the items that apply to that parent menu how do i keep it from displaying duplicate data?
Had the idea of placing it in array then shifting through it? but not sure exactly how i would go about doing that...

Code: Select all

 
function menu($menu)
{
    $newCI =& get_instance();
    //uppercases first letter of menu request
    $request = ucfirst(strtolower($menu));
    //sets up initial mysql query to find menu requested.
    $query = "SELECT * FROM menus WHERE name = '$request'";
    $queryresult = $newCI->db->query($query);
    $menuid = 0;
    //finds the menu requested and pulls the id of the menu along with menu id
    foreach ($queryresult->result() as $row)
    {
        $menuid = $row->id;
        $menuHtml = "<ul id='".$row->css_id."'>";
    }
    //sets up second mysql query to find associated menu items
    $query2 = "SELECT * FROM menu_items WHERE menu_id = $menuid ORDER BY 'order'";
    $queryresult2 = $newCI->db->query($query2);
    //finds all menu items and stores in menu html
    foreach($queryresult2->result() as $menu)
        {
            if($menu->has_sub == 1){
                $menuHtml .= "<li class='mw_menu_cat'><a href='".$menu->href."'>".$menu->name."</a>";
                $menuHtml .= "<ul>";
                $query3 = "SELECT * FROM menu_items WHERE parent = ? ORDER BY 'order'";
                $queryresult3 = $newCI->db->query($query3, array($menu->id));
                foreach($queryresult3->result() as $submenu){
                    if($submenu->has_sub == 1)
                    {
                        //displays parent sub-menu
                        $menuHtml .= "<li class='mw_menu_subcat'><a href='".$submenu->href."'>".$submenu->name."</a>";
                        $menuHtml .= "<ul>";
                        //sets up mysql query to find all submenu of submenu
                        $query4 = "SELECT * FROM menu_items WHERE parent = ? ORDER BY 'order'";
                        $queryresult4 = $newCI->db->query($query4, array($submenu->id));
                        //runs loops through each result for sub-submenu
                        foreach($queryresult4->result() as $sub_submenu){
                            $menuHtml .= "<li><a href='".$sub_submenu->href."'>".$sub_submenu->name."</a></li>";
                        }
                    } else {
                        //display submenu children 
                        $menuHtml .= "<li><a href='".$submenu->href."'>".$submenu->name."</a></li>";
                    }
                }
                $menuHtml .= "</ul></li>";
            } else {
                $menuHtml .= "<li><a href='".$menu->href."'>".$menu->name."</a></li>";
            }
            $menuHtml .= "</ul>";
        }
        return $menuHtml;
}
N/M I got it figured out!
Post Reply