Jump to content

using PHP to set a current ID for nav menu


webguync

Recommended Posts

Hi, I have some code that I think worked at one point, but now it doesn't. I have a side navigation menu which I have set as an include. I want to change CSS styling to a current state to whatever page you are current on. For instance the portfolio link would look like this.

<li><a id="current" href="../Portfolio/" title="Visit my Portfolio">Portfolio</a></li>

 

Both the page I am trying to set this on and my includes are in separate directories, so I am not sure if that is messing things up, but the id="current is not being set with what I currently have as my code.

 

 

Here is my current code.

 

<?php
$menu = <<< MENU
<div id="SideMenu">
<div id="button">
<img src="../images/NavButton.png" width="184" height="32" class="menu_class" />
<ul class="the_menu">
<li><a href="../index.php" title="take me home">Home</a></li>
<li><a  href="../Portfolio/" title="Visit my Portfolio">Portfolio</a></li>
<li><a href="../blog/" title="See what I have to say">Blog</a></li>
<li><a href="../Contact/" title="Get in Touch with me">Contact</a></li>
</ul>
</div><!--end button div-->
</div><!--end SideMenu div-->
MENU;
$lines = split("\n", $menu);
foreach ($lines as $line) {
$current = false;
preg_match('/href="([^"]+)"/', $line, $url);
if (substr($_SERVER["REQUEST_URI"], 0, 5) == substr($url[1], 0, 5)) {
$line = str_replace('<a h', '<a id="current" h', $line);
}
echo $line."\n";
}

?>

 

any ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.