Ik krijg het maar niet voor elkaar om mijn menu items actief te maken. Alles werkt zoals ik het hebben wil zolang er geen items onder parents hangen.
Maar ik wil ook dat de parent active wordt als er een item die daaronder hangt aangeklikt wordt.
Dit is de code, ik hoop dat het een beetje duidelijk is:
<?php function getMenu()
{
// Select all entries from the menu table
$result = mysql_query("SELECT * FROM menu_items WHERE status = 1 ORDER BY parent, sort, label");
$num_rows = mysql_num_rows($result);
// Create a multidimensional array to contain a list of items and parents
$menu = array
(
'items' => array(),
'parents' => array()
);
// Builds the array lists with data from the menu table
$menu_name = '';
while ($items = mysql_fetch_assoc($result))
{
// Creates entry into items array with current menu item id ie. $menu['items'][1]
$menu['items'][$items['id']] = $items;
// Creates entry into parents array. Parents array contains a list of all items with children
$menu['parents'][$items['parent']][] = $items['id'];
$menu_name = 'menu_'.$items['id'];
}
$q = 'SELECT status FROM layouts WHERE position_id = "'.getPositionIdByPositionName('menu').'" AND page_id = "'.getPageIdByPage().'" ';
$result = mysql_query($q) or die(mysql_error());
$menu_status_id = 0;
while ($menu_status = mysql_fetch_assoc($result))
{
$menu_status_id = $menu_status['status'];
}
if ( $menu_status_id == 1 )
{
echo "<div id='menu'>";
echo buildMenu(0, $menu);
echo "</div>";
}
}
// Menu builder function, parentId 0 is the root
function buildMenu($parent, $menu)
{
$html = "";
if (isset($menu['parents'][$parent]))
{
$html .= "
<ul>\n";
foreach ($menu['parents'][$parent] as $itemId)
{
if(!isset($menu['parents'][$itemId]))
{
if (getCurrentPage() == $menu['items'][$itemId]['link'])
{
$active = "class='active'";
}
else
{
$active = '';
}
$html .= "<li ".$active.">\n <a ".$active." href='".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
}
if(isset($menu['parents'][$itemId]))
{
$html .= "
<li ".$active.">\n <a ".$active." href='".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a> \n";
$html .= buildMenu($itemId, $menu);
$html .= "</li> \n";
}
}
$html .= "</ul> \n";
}
return $html;
} ?>