I hate javascript so I just used a prebuilt drop down menu system and created a function to make the lists that it uses.
you can get the js and css from http://www.leigeber.com/2008/11/drop-down-menu/
here is the php function:
function buildMenu( $array=null,$i=0 ) {
$i == 0 ? $menuClass = ' class="menu" id="menu"' : $menuClass = '';
$list = str_repeat( "\t",$i ) . "<ul{$menuClass}>\n";
$i2 = 0;
foreach( (array)$array as $name => $data ) {
$name = ucwords($name);
$subm = '';
$nList = '';
if ( isset( $data['submenu'] ) ) {
$subm = $i !== 0 ? ' class="sub"' : '';
$nList = "\n" . buildMenu( $data['submenu'],( $i + 1 ) ) . "\n" . str_repeat( "\t",( $i + 1 ) );
}
$topl = $i > 0 && $i2 == 0 ? ' class="topline"' : '';
$menu = $i == 0 ? ' class="menulink"' : '';
$list .= str_repeat( "\t",( $i + 1 ) ) . "<li{$topl}><a{$subm}{$menu} href=\"{$data['href']}\" title=\"{$data['title']}\">{$name}</a>{$nList}</li>\n";
$i2++;
}
$list .= str_repeat( "\t",$i ) . "</ul>";
return $list;
}
to use just run buildMenu( $menu ); where $menu is the array i mentioned in an earlier post.
Thanks for the reply,
But what about the function that builds the menu array? That I kinda meant.
When I get pages from database, I get:
$pages = array (
array ("page_id" => 1, "name" => "Page 1", "parent_id" => 0),
array ("page_id" => 2, "name" => "Page 2", "parent_id" => 0),
array ("page_id" => 3, "name" => "Sub 1", "parent_id" => 1),
array ("page_id" => 4, "name" => "Sub 2", "parent_id" => 2),
array ("page_id" => 5, "name" => "Sub of sub", "parent_id" => 3),
) ;
How do I get it into this (into php array):
$pages = array (
array ("page_id" => 1, "name" => "Page 1", "parent_id" => 0,
"children" => array (array ("page_id" => 3, "name" => "Sub 1", "parent_id" => 1))),
array ("page_id" => 2, "name" => "Page 2", "parent_id" => 0,
"children" => array ("page_id" => 4, "name" => "Sub 2", "parent_id" => 2,
"children" => array ("page_id" => 5, "name" => "Sub of sub", "parent_id" => 4))),
);