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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.