I am looking to find a way to highlight a navigation item when selecting submenu item. It has to be done in PHP. The menu is located is loaded as include on each page, dynamically. Also, i am not a programmer so please explain like you would for a beginner.
I have the following menu in include.php file:
<?php
function setActive($name){
global $pageName;
if ($pageName == $name){
echo "class='active' ";
}
}
?>
<ul id="menu">
<li ><a <?php setActive ('A')?> href="a.php">A</a></li>
<li ><a <?php setActive ('B')?> href="a.php">B</a></li>
<li>
<a href="#" >C</a>
<ul>
<li><a href="D.php">D</a></li>
<li><a href="E.php" >E</a></li>
</ul>
</li>
<li>
</ul>
Each page has the following:
<?php
$pageName ='D';
require "include.php";
?>
How can I make 'active' the main menu item C when either D or E are selected?
Thank you.
There are way better ways to do this, with a proper menu stucture, but using the system you have already got, you could do something like this:
<?php
function setActive($name) {
global $pageName;
if ($pageName == $name){
echo "class='active' ";
}
}
function setMultiActive($array_of_names) {
global $pageName;
foreach ($array_of_names as $name) {
if ($pageName == $name) {
echo "class='active'";
break;
}
}
?>
<ul id="menu">
<li ><a <?php setActive ('A')?> href="a.php">A</a></li>
<li ><a <?php setActive ('B')?> href="a.php">B</a></li>
<li>
<a href="#" <?php setMultiActive(['D', 'E']) ?>>C</a>
<ul>
<li><a href="D.php">D</a></li>
<li><a href="E.php" >E</a></li>
</ul>
</li>
<li>
</ul>