Jump to content

php and navigation with CSS


webguync

Recommended Posts

I want to use CSS to indicate that a user is on a certain page, while also using a menu as an include, and I was able to get this working once, but now it does not want to.

 

the code for the menu I have:

<?php
$footer = <<< MENU
<ul>
          <li><a href="what-we-do.php" title="What We Do">What we do</a></li>
          <li><a href="news-events.php" title="News & Events">News & Events</a></li>
          <li><a href="affiliates-programs.php" title="Affiliates & Programs">Affiliates & Programs</a></li>
          <li><a href="people.php" title="People">People</a></li>
          <li><a href="facilities.php" title="Facilities">Facilities</a></li>
          <li><a href="tech-resources.php" title="Technology & Resources">Technology & 
            Resources</a></li>
          <li><a href="site-map.php" title="web site map">Web Site Map</a></li>
          <li><a href="copyright-disclaimer.php" title="Copyright & Displaimer">Copyright & Disclaimer</a></li>
          <li class="last"><a href="privacy-policy.php" title="Privacy Policy">Privacy Policy</a></li>
        </ul>
MENU;
$lines = split("\n", $footer);
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";
}

?>

 

and in the CSS I have a#current set to a particular color. This worked previously, but now it does not. the current state doesn't appear in the html. Any ideas why?

Link to comment
Share on other sites

to elaborate on this, what I am trying to do, is create dynamically in the HTML using PHP

<li><a id="current" href="what-we-do.php" title="What We Do">What we do</a></li>

 

and then the CSS will do the rest, creating the "you are on this page" indicator. This needs to be done dynamically so that I can use the menu code as an include, and not have to hard code on every page.

Link to comment
Share on other sites

That's way too complicated. IF you're using regular files names (i.e. index.php or contact.php and not index.php?page=about) then you should just simply add an id to your body tag and a class on each menu link.

 

#home .home, #about .about, #contact .contact {
text-decoration: underline;
}

 

So each page has a unique id and then you can use the exact same nav on every page. You can even abstract it out into it's own file and include it only having to change it in one place.

Link to comment
Share on other sites

That's way too complicated.

 

You're right, the CSS method is by far the best for small websites with a few pages. I use it on my websites.

 

Just assumed that because he was asking for a php solution that he must be building a more complicated site that required a similar solution to the one I had to employ. When those sites have dozens of pages that are grouped under a handful of menu categories, then only practical way to highlight which page is currently being viewed is by using php_self.

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.