Been working on a simple script that generates a website navigation bar from an array, I'm fairly happy with the results and its ability to handle multiple sub menus, however the one thing I cannot figure out and could do with a little bit of help on is how to configure the array so a menu that has a sub menu can also have a link as well. Current code
Many thanks
Ian
4x4 UK | The Ultimate Off Road Club for all types of 4x4
Code:
<?php
// The menu bar is built from the array below.
$pageArray = array(
"Home" => array(
"How to join" => "/index.php?opt=1",
"Legal" => array(
"Copyright Information" => "index.php?opt=2",
"Privacy Statement" => "index.php?opt=3",
"Disclaimer" => "index.php?opt=4",
"Club Rules" => "index.php?opt=5",),
"Contact Us" => "index.php?opt=6&ec=info",
),
"Calendar" => "/calendar/",
"Classifieds" => "/classifieds/",
"Info" => array(
"Downloads" => "/downloads/",
"Calculators" => array(
"Tyre Size Calculator" => "index.php?opt=6",
"Wheel Offset Calculator" => "index.php?opt=7",
"Gear Ratio Calculator" => "index.php?opt=8",)
),
"Forums" => "/forum/",
"Photo Gallery" => "/photos/",
);
function navGen($pageArray) {
// Walk through $pageArray and generate the navigation list.
echo "<ul>\n";
foreach ($pageArray as $name => $url) {
if (is_array($url)) {
echo "<li>".$name."\n";
navGen($url);
echo"</li>\n";
} else {
echo "<li><a href=\"$url\">".$name."</a></li>\n";
}
}
echo "</ul>\n";
}
?>
<div id="navbar">
<?= navGen($pageArray); ?>
</div>
Ian
4x4 UK | The Ultimate Off Road Club for all types of 4x4