10-18-2012, 11:14 AM
|
#1 (permalink)
|
|
The Visitor
Join Date: Oct 2012
Posts: 2
Thanks: 0
|
Multilevel php menu
Hello.
I'm trying to make an multilevel menu. But the way i wan't it to work seems to be a bit complicated for me. I wan't the menu to work like this one(left menu): http://www.proconsult.dk/. The page must reload every time a menu has been clicked, because i wan't a new page to open (newest offers related to the clicked category).
So far i managed to get a non collapsable menu script:
PHP Code:
<?php
// get all menuitems with 1 query
$result = mysql_query("SELECT cat_id, cat_parent, cat_name FROM categories ORDER BY cat_id, cat_name ");
// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
while ($menuItem = mysql_fetch_assoc($result))
{
$menuData['items'][$menuItem['cat_id']] = $menuItem;
$menuData['parents'][$menuItem['cat_parent']][] = $menuItem['cat_id'];
}
// menu builder function, parentId 0 is the root
function buildMenu($parentId, $menuData)
{
$html = '';
if (isset($menuData['parents'][$parentId]))
{
$html = '<ul>';
foreach ($menuData['parents'][$parentId] as $itemId)
{
$html .= '<li>' . $menuData['items'][$itemId]['cat_name'];
// find childitems recursively
$html .= buildMenu($itemId, $menuData);
$html .= '</li>';
}
$html .= '</ul>';
}
return $html;
}
// output the menu
echo buildMenu(0, $menuData);
?>
Just so you know. I don't want to use any Jquery, the menu must be pure php, if possible 
Thank you in advance.
Regards, Simon
|
|
|
|