webguync Posted July 19, 2007 Share Posted July 19, 2007 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? Quote Link to comment Share on other sites More sharing options...
webguync Posted July 19, 2007 Author Share Posted July 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
bronzemonkey Posted July 23, 2007 Share Posted July 23, 2007 This is a modified version of the code I'm using. $page = $_SERVER['PHP_SELF']; <a href="whatever.php"<?php if($page == 'whatever.php' ) { echo ' class="current"';} ?>>Whatever</a> Not ideal but no one gave me any better solutions...hope it gets the job done. Quote Link to comment Share on other sites More sharing options...
bronzemonkey Posted July 23, 2007 Share Posted July 23, 2007 I should add that echo $_SERVER['PHP_SELF'] would output /directory/filename.php If you want only the basename of the file use: echo basename($_SERVER['PHP_SELF']); or for the directory only use: echo dirname($_SERVER['PHP_SELF']); Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 23, 2007 Share Posted July 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
bronzemonkey Posted July 23, 2007 Share Posted July 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.