[eluser]skunkbad[/eluser]
[quote author="jedd" date="1259915600"]For anyone trying to follow that link - manually change the '#' to '% 23' (no space).
By the way - will your data have sub-menus that contain both leaf items as well as sub-menus themselves, or will it always be a collection of one or the other only?[/quote]
I don't know what these terms are, leaf vs. sub-menus.
I've got a good start on what I'm working on though. Just some procedural stuff:
Code:
<?php
$i = array();
$i[] = array('Examples' ,1 ,0);
$i[] = array('Widgets' ,2 ,1);
$i[] = array('Things' ,3 ,0);
$i[] = array('Shiny' ,4 ,3);
$i[] = array('Metal' ,5 ,4);
$i[] = array('Silver' ,6 ,5);
foreach($i as $a)
{
// if top level
if($a[2] == 0)
{
// create a top level category
$menu[$a[0]] = array(
'category_id' => $a[1]
);
}
else
{
foreach($menu as $b => $c)
{
if($c['category_id'] == $a[2])
{
$menu[$b]['sub'][$a[0]] = array(
'category_id' => $a[1]
);
break;
}
else if(array_key_exists('sub', $c))
{
foreach($c['sub'] as $d => $e)
{
if($e['category_id'] == $a[2])
{
$menu[$b]['sub'][$d]['sub'][$a[0]] = array(
'category_id' => $a[1]
);
break;
}
else if(array_key_exists('sub', $e))
{
foreach($e['sub'] as $f => $g)
{
if($g['category_id'] == $a[2])
{
$menu[$b]['sub'][$d]['sub'][$f]['sub'][$a[0]] = array(
'category_id' => $a[1]
);
break;
}
}
}
}
}
}
}
}
function buildMenu($menu_array, $is_sub=FALSE)
{
/*
* If the supplied array is part of a sub-menu, add the
* sub-menu class instead of the menu ID for CSS styling
*/
$attr = (!$is_sub) ? ' id="menu"' : ' class="submenu"';
$menu = "<ul$attr>\n"; // Open the menu container
/*
* Loop through the array to extract element values
*/
foreach($menu_array as $id => $properties) {
/*
* Because each page element is another array, we
* need to loop again. This time, we save individual
* array elements as variables, using the array key
* as the variable name.
*/
foreach($properties as $key => $val) {
/*
* If the array element contains another array,
* call the buildMenu() function recursively to
* build the sub-menu and store it in $sub
*/
if(is_array($val))
{
$sub = buildMenu($val, TRUE);
}
/*
* Otherwise, set $sub to NULL and store the
* element's value in a variable
*/
else
{
$sub = NULL;
$$key = $val;
}
}
/*
* If no array element had the key 'url', set the
* $url variable equal to the containing element's ID
*/
if(!isset($url)) {
$url = $id;
}
/*
* Use the created variables to output HTML
*/
$menu .= "<li><a href='$url'>$url</a>$sub</li>\n";
/*
* Destroy the variables to ensure they're reset
* on each iteration
*/
unset($url, $display, $sub);
}
return $menu . "</ul>\n";
}
echo buildMenu($menu, FALSE);