Jump to content

Multi Level Menu Help


johnrb87

Recommended Posts

Hi all

 

I have a menu function which basically produces a menu which looks like

 

Products
    Apple
       iMac
       iPod
       iPhone
    Microsoft
       Windows
       Office

 

and the code I use is;

 

THE FUNCTION

function menu($parentID, $mymenu)
{
   $html = "";
   if (isset($mymenu['parentID'][$parentID]))
   {
      $html .= "
      <ul>\n";
       foreach ($mymenu['parentID'][$parentID] as $menu_id)
       {
          if(!isset($mymenu['parentID'][$menu_id]))
          {
             $html .= "<li>\n  <a href='/".$mymenu['menu_item'][$menu_id]['url']."'>".$mymenu['menu_item'][$menu_id]['value']."</a>\n</li>";
          }
          if(isset($mymenu['parentID'][$menu_id]))
          {
             $html .= "<li>\n  <a href='/".$mymenu['menu_item'][$menu_id]['url']."'>".$mymenu['menu_item'][$menu_id]['value']."</a>";
             $html .= menu($menu_id, $mymenu);
             $html .= "</li>";
          }
       }
       $html .= "</ul>";
   }
   return $html;
}

 

CREATE MENU CODE

$result = mysql_query("SELECT id, value, url, parentID FROM menu WHERE active = 1 AND deleted = 1 ORDER BY position ASC");
$mymenu = array('menu_item' => array(),'parentID' => array());
while ($menu_item = mysql_fetch_assoc($result))
{
$mymenu['menu_item'][$menu_item['id']] = $menu_item;
$mymenu['parentID'][$menu_item['parentID']][] = $menu_item['id'];
}

echo menu(0, $mymenu);

 

The problem I have is the URLS, at the moment the menu URLS are outputted as

 

Products  -  http://localhost/Products
     Apple  -  http://localhost/Apple
       iMac  -  http://localhost/iMac
       iPod  -  http://localhost/iPod

 

But I need to alter my function so that URLS are outputted as

 

Products  -  http://localhost/Products
     Apple  -  http://localhost/Products/Apple
       iMac  -  http://localhost/Products/Apple/iMac
       iPod  -  http://localhost/Products/Apple/iPod

 

Is this at all possible?

 

Thanks very much everyone

 

John

Link to comment
https://forums.phpfreaks.com/topic/223583-multi-level-menu-help/
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.