See demo.
In the example below I create an associative array which contains two elements:
- The name of the page which the navigation points to.
- The name which should appear in the navigation.
I then grab the filename of the of the current page in the browser.
I then traverse the associative array, each time comparing the filename against the current file shown in the browser, each time creating an anchor using the values.
If array filename and current page filename are the same, then I add a CSS ID to the anchor tag.
You will need to write a little CSS to highlight the current page anchor such as:
nav a#activeMenuItem
{
background:#EEEEEE;
}
But other than that, it works like a charm. Have fun!
<nav>
<?php
$navArray = array('index.php'=>'home', 'services.php'=>'services','portfolio.php'=>'portfolio','contact.php'=>'contact');
$fileName = substr(strrchr($_SERVER['SCRIPT_NAME'],47),1);
foreach($navArray as $fname => $linktitle)
{
echo '<a href="'.$fname.'"';
if($fname==$fileName)
{
echo ' id="activeMenuItem"';
}
echo '>'.$linktitle.'</a>';
}
?>
</nav>
No comments:
Post a Comment