Question: How to create a navigation that allows you to apply different classes to different subitems from a multidimensional array?
Here's how I did it before I needed layered navigation:
Home Pics About
and was called by nav ():
function nav(){ $links = array( "Home" => "home.php", "Pics" => "pics.php", "About" => "about.php" ); $base = basename($_SERVER['PHP_SELF']); foreach($nav as $k => $v){ echo buildLinks($k, $v, $base); } }
Here is the buildLinks ():
function buildLinks($name, $page, $selected){ if($selected == $page){ $theLink = "<li class=\"selected\"><a href=\"$page\">$name</a></li>\n"; } else { $thelink = "<li><a href=\"$page\">$name</a></li>\n"; } return $thelink; }
My question is again:
How could I achieve the following navigator (and notice that visible sub-navigation elements are only present on this particular page):
Home something1 something2 Pics About
and...
Home Pics people places About
What i tried
From looking at it, it seems that some iterator in SPL would be good for this, but I'm not sure how to approach this. I played with RecursiveIteratorIterator, but I'm not sure how to apply a different style only to the submenu items, and also how to show these items only if you are on the right page.
I built this array for testing, but I don’t know how to work with submenu items individually:
$nav = array( array( "Home" => "home.php", "submenu1" => array( "something1"=>"something1.php", "something2" => "something2.php") ), array("Pics" => "pics.php"), array("About" => "about.php") );
Next, the batch will be printed in order, but how can I apply, say, a class name to submenu items or show them only when a person is turned on, say, on the "Home" page?
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($nav)); foreach($iterator as $key=>$value) { echo $key.' -- '.$value.'<br />'; }
And this is me:
Home something1 something2 Pics About
But I don’t have the opportunity to apply classes to these subitems and I can’t display them only conditionally, because I don’t see how to focus only on these elements.