Hello all,
I’ve been struggling with this for a week and cannot seem to figure out how to do it (or find the answer I’ve been looking for).
So I’m trying to build a flexible contextual navigation menu that can accept at least 2 tiers (nav and subnav).
Essentially, I want to build a semantic html navigation list:
<ul id="nav">
<li>Item One</li>
<li>Item Two</li>
<li>
Item Three
<ul>
<li>Item Three Sub 1</li>
<li>Item Three Sub 2</li>
</ul>
</li>
<li>Item Four</li>
</li>
Item Five
<ul>
<li>Item Five Sub 1</li>
<li>Item Five Sub 2</li>
</ul>
</li>
</ul>
Note: I’ve got this menu setup so that if you are not in a directory, when you rollover an item, if it has a submenu, it will display. But I also want to be able to display the submenu if you’re on a page that’s a child of that particular directory.
Here’s how I’m building it in PHP:
$tierOne['Item One'] = "/itemone.php";
$tierOne['Item Two'] = "/itemtwo.php";
$tierOne['Item Three'] = "/itemthree/index.php";
$tierOne['Item Four'] = "/itemfour.php";
$tierOne['Item Five'] = "/itemfive/index.php";
$tierTwo['Item Three']['Item Three Sub 1'] = "/itemthree/sub1.php";
$tierTwo['Item Three']['Item Three Sub 2'] = "/itemthree/sub2.php";
$tierTwo['Item Five']['Item Three Sub 1'] = "/itemfive/sub1.php";
$tierTwo['Item Five']['Item Three Sub 2'] = "/itemfive/sub2.php";
$htmlMenu = '<ul>'; // start the tier one nav
foreach($tierOne as $key => $value) {
if($_SERVER['PHP_SELF'] == $value) { //if the current page url is equal to the url of the array index value, then give the list item a class of 'activePage' (for styling, etc)
$activeClass = ' class="activePage"';
}
else {
$activeClass = "";
}
$htmlMenu .= "<li><a href=\"$value\">$key2</a>"
if(isset($key[$tierTwo])) { // if a tier one item has sub array
$htmlMenu .= "<ul>"; // start the tier two nav (here's where my problem is. how can i figure out which active directory i'm in so i can give the subnav ul a class of 'active' so i can make that subnav display whenever i'm in a directory that has pages??
foreach($tierTwo as $key2 => $value2) {
$htmlMenu .= "<li><a href=\"$value2\">$key2</a></li>"
}
$htmlMenu .= "</ul></li>" // end the tier two nav and close off the tier one item
}
else {
$htmlMenu .= "</li>"; // otherwise close the tier one item
}
}
$htmlMenu .= "</ul>"; // end the tier one nav
Might not be the most elegant solution. But I’m getting closer. So the bottom line is. I want the navigation to be contextual – that is to know which page and directory its in so it can give ONLY second tier <ul> a class (so that i can keep it displayed).
Any ideas? Thanks!