Let's say that we have many php pages that share the same `navigation.inc.php` as an "include" file.
Even though the navigation include file is shared we want it to be dynamic enough to highlight the menu bar based on the page that is accessing the include file.
In this example we will only deal with one page - the `About` page, but the same paradigm is followed for all pages.
In the `about.php` page we simply set the `active` variable before the navigation page is included...
<?php $active = "about"; ?>
<?php include("navigation.inc.php"); ?>
Then in the navigation page we construct an if condition based on the `active` variable...
<?php
if ($active == "about") {
echo '<li class="current active dropdown mega sub-hidden-collapse" data-id="103" data-level="1" data-hidewcol="1">';
} else {
echo '<li class="dropdown mega sub-hidden-collapse" data-id="103" data-level="1" data-hidewcol="1">';
}
?>
<a class=" dropdown-toggle" href="about-us.php" data-target="#" data-toggle="dropdown">About Us <b class="caret"></b></a>
As you can see, the only difference between the two "li" elements in the if condition is that one uses the class "current active" while the other does not. So when the `about` page includes the `navigation` page, the `about` menu item
will be highlighted as the currently active page.
There are a variety of ways to do this, and i am by no means suggesting that this is the best way. I did, however, find it a helpful and interesting way.
Here's another method to consider for accomplishing the same task...
In `about.php` set an "about" variable to the text that you will need in the navigation page to make the `about` menu item highlighted as active...
<?php $about = "current active"; ?>
<?php include("navigation.inc.php"); ?>
Then in the navigation file simply call the variable $about, which will have the needed text if the about page is the one including the navigation file OR it will be blank when another page is the one including the navigation file...
<?php
echo '<li class="' . $about . ' dropdown mega sub-hidden-collapse" data-id="103" data-level="1" data-hidewcol="1">';
?>
<a class=" dropdown-toggle" href="about-us.php" data-target="#" data-toggle="dropdown">About Us <b class="caret"></b></a>
Then for each of the other pages you would simply change the name of the variable to the page name, and reference that variable in the appropriate section of the navigation page...
`home` page...
<?php $home = "current active"; ?>
<?php include("navigation.inc.php"); ?>
`navigation` page (home link section)...
<?php
echo '<li class="' . $home . '" >';
?>
<a href="index.php" data-target="#">Home</a>