Very much a novice with PHP.
I’ve written a bit of code to generate the html for a horizontal nav menu with drop downs.
What I have works, but i’m sure it’s over engineered and badly written.
Any advice would be greatly appreciated.
This is the code
<ul id = 'nav'>
<?php
$currPage = basename($_SERVER['SCRIPT_FILENAME'], '.php');
$pages = array( 'index',
'gallery' => array(
'critters',
'product viz',
'architecture',
'characters',
'misc'
),
'profile' => array(
'test1',
'test2'
),
'contact',
'tutorials',
'links'
);
foreach ($pages as $parentMenu => $page){
echo(" <li>");
// If no submenu (Not array)
if (!is_array($page)) {
echo( "<a href='$page.php'"
. ( ($page === $currPage) ? " class = 'active'>" : '>' )
. ( ($page === 'index') ? 'home' : $page )
. "</a>");
// Submenu
} else {
// Using a for loop as I need to add a class to the last index
$arrLen = count($page);
echo( "<a href='$parentMenu.php'"
. ( ($parentMenu === $currPage) ? " class = 'active'>" : '>' )
. "$parentMenu</a>\
<ul>\
" );
for ($i = 0; $i < $arrLen; $i++) {
echo(" <li>"
. "<a href='$parentMenu.php?catID=" . ($i+1)
. ( ($i === $arrLen-1) ? "' class = 'last'>" : "'>" )
. "$page[$i]</a></li>\
" );
}
echo (" </ul>\
");
}
echo ("</li>\
");
}
?>
</ul>
and the output
<ul id = 'nav'>
<li><a href='index.php' class = 'active'>home</a></li>
<li><a href='gallery.php'>gallery</a>
<ul>
<li><a href='gallery.php?catID=1'>critters</a></li>
<li><a href='gallery.php?catID=2'>product viz</a></li>
<li><a href='gallery.php?catID=3'>architecture</a></li>
<li><a href='gallery.php?catID=4'>characters</a></li>
<li><a href='gallery.php?catID=5' class = 'last'>misc</a></li>
</ul>
</li>
<li><a href='profile.php'>profile</a>
<ul>
<li><a href='profile.php?catID=1'>test1</a></li>
<li><a href='profile.php?catID=2' class = 'last'>test2</a></li>
</ul>
</li>
<li><a href='contact.php'>contact</a></li>
<li><a href='tutorials.php'>tutorials</a></li>
<li><a href='links.php'>links</a></li>
</ul>
Thanks ahead.
RLM