("SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC");
while ($obj = $items->fetch_object()) {
if ($obj->parent_id == 0) {
$parent_menu[$obj->id]['label'] = $obj->label;
$parent_menu[$obj->id]['link'] = $obj->link_url;
} else {
$sub_menu[$obj->id]['parent'] = $obj->parent_id;
$sub_menu[$obj->id]['label'] = $obj->label;
$sub_menu[$obj->id]['link'] = $obj->link_url;
if (empty($parent_menu[$obj->parent_id]['count'])) {
$parent_menu[$obj->parent_id]['count'] = 0;
}
$parent_menu[$obj->parent_id]['count']++;
}
}
$items->close();
$db->close();
function dyn_menu($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "subnav", $extra_style = "foldout") {
$menu = "
\n";
foreach ($parent_array as $pkey => $pval) {
if (!empty($pval['count'])) {
$menu .= " \n";
} else {
$menu .= " - ".$pval['label']."
\n";
}
if (!empty($_REQUEST[$qs_val])) {
$menu .= "\n";
foreach ($sub_array as $sval) {
if ($pkey == $_REQUEST[$qs_val] && $pkey == $sval['parent']) {
$menu .= "- ".$sval['label']."
\n";
}
}
$menu .= "
\n";
}
}
$menu .= "
\n";
return $menu;
}
function dyn_menu_folded($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "subnav", $extra_style = "foldout") {
$menu = "\n";
foreach ($parent_array as $pkey => $pval) {
if (!empty($pval['count'])) {
$menu .= " \n";
} else {
$menu .= " - ".$pval['label']."
\n";
}
//if (!empty($_REQUEST[$qs_val])) {
$menu .= "\n";
foreach ($sub_array as $sval) {
if ($pkey == $sval['parent']) { //
$menu .= "- ".$sval['label']."
\n";
}
}
$menu .= "
\n";
//}
}
$menu .= "
\n";
return $menu;
}
function rebuild_link($link, $parent_var, $parent_val) {
$link_parts = explode("?", $link);
$base_var = "?".$parent_var."=".$parent_val;
if (!empty($link_parts[1])) {
$link_parts[1] = str_replace("&", "##", $link_parts[1]);
$parts = explode("##", $link_parts[1]);
$newParts = array();
foreach ($parts as $val) {
$val_parts = explode("=", $val);
if ($val_parts[0] != $parent_var) {
array_push($newParts, $val);
}
}
if (count($newParts) != 0) {
$qs = "&".implode("&", $newParts);
}
return $link_parts[0].$base_var.$qs;
} else {
return $link_parts[0].$base_var;
}
}
?>
Dynamic PHP navigation list example page
Dynamic PHP navigation list tutorial (example)
The unordered list below is generated by the code used in the tutorial. Click the main items (Job groups) to make the sub-items visible.
We created also a second menu which shows all sub items, to get this effect we removed the code $pkey == $_REQUEST[$qs_val] && from the second foreach loop and the if statement if (!empty($_REQUEST[$qs_val])).
Example (modified script)
Example (orig. script)
© 2004- by finalwebsites.com