mrittman Posted July 23, 2009 Share Posted July 23, 2009 Hey guys I have a website that I have split up into separate php files. For example I have the header div in its own header.php file, the navigation bar in linkbar.php, etc. All I want to do is change the background image of the button in the linkbar to reflect the current page the user is on. This is very easy to do in CSS and it works great with classes...but now that I have separate php files, it makes things more difficult. Here is the code I am working with: <div id="linkbar"> <div id="navlist"> <ul> <li <?php RenderClass("index.php"); ?> ><a href="index.php">home</a></li> <li <?php RenderClass("about.php"); ?> ><a href="about.php">about dave</a></li> <li <?php RenderClass("news.php"); ?> ><a href="news.php">news</a></li> <li><a href="issues.php">issues</a></li> <li><a href="contribute.php">contribute</a></li> <li><a href="events.php">events</a></li> <li><a href="involved.php">get involved</a></li> <li><a href="links.php">links</a></li> <li><a href="links.php"></a></li> <li class="last"><a href="contact.php">contact</a></li> </ul> </div> </div> <?php function RenderClass($name) { if(curPageURL() == $name) { return "class=\"current\""; } } function curPageURL() { return substr($_SERVER["REQUEST_URI"], 1); } ?> The first three links should work but they're not. Anyone have any ideas? Thanks! Matt Link to comment https://forums.phpfreaks.com/topic/167184-breaking-a-website-up-with-php-and-outputting-current-page-css-in-navigation-bar/ Share on other sites More sharing options...
o3d Posted July 23, 2009 Share Posted July 23, 2009 <li <?php RenderClass("index.php"); ?> ><a href="index.php">home</a></li> <li <?php echo RenderClass("index.php"); ?> ><a href="index.php">home</a></li> Remember to echo what RenderClass returns. If you echo'ed in RenderClass, then you only need to modify the RenderClass function. Link to comment https://forums.phpfreaks.com/topic/167184-breaking-a-website-up-with-php-and-outputting-current-page-css-in-navigation-bar/#findComment-881494 Share on other sites More sharing options...
mrittman Posted July 23, 2009 Author Share Posted July 23, 2009 Nevermind, already found an answer to this! Here is the working code for anyone that might find this beneficial: <div id="linkbar"> <div id="navlist"> <ul> <li<?php RenderClass("/index.php/"); ?>><a href="index.php">home</a></li> <li<?php RenderClass("/about.php/"); ?>><a href="about.php">about dave</a></li> <li<?php RenderClass("/news.php/"); ?>><a href="news.php">news</a></li> <li<?php RenderClass("/issues.php/"); ?>><a href="issues.php">issues</a></li> <li<?php RenderClass("/contribute.php/"); ?>><a href="contribute.php">contribute</a></li> <li<?php RenderClass("/events.php/"); ?>><a href="events.php">events</a></li> <li<?php RenderClass("/involved.php/"); ?>><a href="involved.php">get involved</a></li> <li<?php RenderClass("/links.php/"); ?>><a href="links.php">links</a></li> <li<?php RenderClass("/contact.php/"); ?> class="last"><a href="contact.php">contact</a></li> </ul> </div> </div> <?php function RenderClass($name) { if(preg_match($name, $_SERVER['PHP_SELF'])){ echo ' class="current"'; } } ?> Link to comment https://forums.phpfreaks.com/topic/167184-breaking-a-website-up-with-php-and-outputting-current-page-css-in-navigation-bar/#findComment-881498 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.