<pre><?php
mysql_connect("localhost", "root", "");
mysql_select_db("menu");
$items = array();
$itemchildren = array();
# suck in everything from mysql into two arrays
$sql = mysql_query("select * from menu");
while ($item = mysql_fetch_array($sql))
{
$items[$item["id"]] = $item;
if (!isset($itemchildren[$item["parent_id"]]))
{
$itemchildren[$item["parent_id"]] = array();
}
$itemchildren[$item["parent_id"]][] = $item["id"];
}
# show a list of item IDs, and IDs of their children
print_r($itemchildren);
# print the menu, starting at the top (id=0)
print build_menu($items, $itemchildren, 0);
function build_menu($items, $itemchildren, $this_id)
{
$html = "";
# print either the start of a menu, or a link (and some CSS to indent child links)
if ($this_id>0)
{
if ($items[$this_id]["is_link"]==1)
{
$html.= "- <a href=\"{$items[$this_id]["link"]}\">{$items[$this_id]["title"]}</a><div style=\"padding-left:5px\">";
} else
{
$html.= "<div style=\"border:1px black solid;padding:2px;background-color:#eee\"><b>{$items[$this_id]["title"]}</b><br/>";
}
}
# print everything in menu, or all the child links
if (isset($itemchildren[$this_id]))
{
foreach ($itemchildren[$this_id] as $child_id)
{
$html.= build_menu($items, $itemchildren, $child_id);
}
}
# print the end of the menu, or the end of a link's CSS padding bit
if ($this_id>0)
{
if ($items[$this_id]["is_link"]==1)
{
$html.= "</div>";
} else
{
$html.= "</div><br/>";
}
}
return $html;
}