Hope this will get you started, it is a menu-builder that I wrote a while ago. It works. Not sure about glitches (Been too long..):
<?
$menuquery = "select * from menu order by M_sortorder asc";
$result=mysql_query($menuquery) or die('An error occured retrieving data');
$listing = Array();
while($row = mysql_fetch_array($result))
{
// This id: The page identifier
// Path: the link base path
// parent id: table ID of the parent in the tree
// category label: Label in the menu
// M_link: Actual page name (I think! check code)
$listing[] = array("This_id" => $row['M_page_nr'],
"path" => $row['M_path'],
"parent_id" => $row['M_p_nr'],
"category_title" => $row['M_label'],
"link" => $row['M_link']
);
}
function get_expanded($parent, $level, $array, $currentmain)
{
global $pageid; // Just lazy: Did not want to pass the current page id
$selectbranch = '';
foreach($array AS $node)
{
if($node['parent_id']==$parent)
{
if($node['parent_id'] == 0) // Set the branch we are evaluating
{
$currentmain = $node['This_id'];
}
if($pageid <> $node['This_id'])
{
$ttt = get_expanded($node['This_id'], ($level + 1), $array, $currentmain);
if($ttt <> "")
{
$selectbranch = $ttt;
}
}
else
{
$selectbranch = $currentmain;
}
}
}
return $selectbranch;
} // end function
function display_tree($parent, $level, $array, $path, $expanded, $isexpanded)
{
global $pageid, $countrylinks;
$html = "";
foreach($array AS $node)
{
if($node['parent_id']==$parent) // The current entry is a subbranch
{
if($node['parent_id'] == 0) // Reset the expanded branch
{
$isexpanded = -1;
}
if(($node['This_id']) == $expanded && ($node['This_id'] <> 12)) // SET the expanded branch
{
$isexpanded = 1;
}
if($pageid <> $node['This_id'])
{
if(($isexpanded <> -1) or ($level <=0))
{
$html .= "
<a class=\"menu".$level."\" href=\"$path$node[path]$node[link]\">".$node['category_title']."</a>
";
$html .= display_tree($node['This_id'], ($level + 1), $array, $path, $expanded, $isexpanded);
}
}
else
{
$html .= "
<a id=\"current\" class=\"menu".$level."\" href=\"$path$node[path]$node[link]\">".$node['category_title']."</a>
";
$html .= display_tree($node['This_id'], ($level + 1), $array, $path, $expanded, $isexpanded);
}
}
}
// }
return $html;
} // end display list
// Now use it
$opened = get_expanded(0, 0, $listing, -2);
$menu = display_tree(0, 0, $listing, $baseurl, $opened, -1);
$menu .="<a class=\"menu0\" href=\"javascript:window.print()\">Print Page</a>" ;
?>