ID label link parent sort
CREATE TABLE `menu` (
`id` int(11) NOT NULL auto_increment,
`label` varchar(50) NOT NULL default '',
`link` varchar(100) NOT NULL default '#',
`parent` int(11) NOT NULL default '0',
`sort` int(11) default NULL,
PRIMARY KEY (`id`),
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
ID label link parent sort
1 Home #home 0 0
2 Code #code 0 0
3 Contact #contact 0 0
4 PHP #php 2 0
5 CSS #css 2 0
6 Scripts #scripts 4 0
7 Help #help 4 0
8 Archive #archive 6 0
9 Snippet #snippet 8 0
function display_children($parent, $level) {
$result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
echo "<ul>";
while ($row = mysql_fetch_assoc($result)) {
if ($row['Count'] > 0) {
echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a>";
display_children($row['id'], $level + 1);
echo "</li>";
} elseif ($row['Count']==0) {
echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
} else;
}
echo "</ul>";
}
$result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
ID label link count
1 Home #home 0
2 Code #code 2
3 Contact #contact 0
while ($row = mysql_fetch_assoc($result)) {
if ($row['Count'] > 0) {
echo "<li><a href='". $row['link'] ."'>". $row['label'] ."</a>";
display_children($row['id'], $level + 1);
echo "</li>";
} elseif ($row['Count']==0) {
echo "<li><a href='". $row['link'] ."'>". $row['label'] ."</a></li>";
} else;
}
display_children(0, 1);
display_children(4, 1);
echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
echo "<li class='list'><a class='list_link' href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
echo "<li class='level".$level."'><a class='level".$level."' href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
.menu ul{color:#FFF;} /* Main container, includes the background of the static portion of the menu */
.menu ul li{color:#FFF;} /* This is the style for the main menu items */
.menu ul ul{color:#FFF;} /* This is the container for the first submenu */
.menu ul ul li{color:#FFF;} /* This is the style for the submenus */
// Select all entries from the menu table
$result=mysql_query("SELECT id, label, link, parent FROM menu ORDER BY parent, sort, label");
// Create a multidimensional array to conatin a list of items and parents
$menuData = array(
'items' => array(),
'parents' => array()
);
// Builds the array lists with data from the menu table
while ($menuItem = mysql_fetch_assoc($result))
{
// Creates entry into items array with current menu item id ie. $menuData['items'][1]
$menuData['items'][$menuItem['id']] = $menuItem;
// Creates entry into parents array. Parents array contains a list of all items with children
$menuData['parents'][$menuItem['parent']][] = $menuItem['id'];
}
// Menu builder function, parentId 0 is the root
function buildMenu($parent, $menuData)
{
$html = "";
if (isset($menuData['parents'][$parent]))
{
$html .= "<ul>\n";
foreach ($menuData['parents'][$parent] as $itemId)
{
if(!isset($menuData['parents'][$itemId]))
{
$html .= "<li>\n <a href='".$menuData['items'][$itemId]['link']."'>".$menuData['items'][$itemId]['label']."</a>\n </li>\n";
}
if(isset($menuData['parents'][$itemId]))
{
$html .= "<li>\n <a href='".$menuData['items'][$itemId]['link']."' >".$menuData['items'][$itemId]['label']."</a> \n";
$html .= buildMenu($itemId, $menuData);
$html .= "</li>\n";
}
}
$html .= "</ul>\n";
}
return $html;
}
echo buildMenu(0, $menuData);
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)