Dilb Posted June 20, 2007 Share Posted June 20, 2007 Hi, I'm new to php, and I'm struggling to find out how to use php to set a class based on certain criteria. Basically, I'm using a file called navigation.html to store my primary navigation links as a list, and I'm using php's include once to access this in my pages. Obviously this means that I only need to change the navigation once to change it on all pages. Trouble is I need to highlight the current page so that as users navigate around they know where they are. Is there some way I can use php to apply a class or id to the navigation link that points to the current page? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
trq Posted June 20, 2007 Share Posted June 20, 2007 s there some way I can use php to apply a class or id to the navigation link that points to the current page? Yes.... can we see navigation.html? (By the way, you'll need to make this a php file). Quote Link to comment Share on other sites More sharing options...
Dilb Posted June 21, 2007 Author Share Posted June 21, 2007 Hi there, An example of one of my pages might be: <body> <h2>Navigation</h2> <ul> <?php include("navigation.html"); ?> </ul> </body> The 'navigation.html' file would then just be individual list items, such as: <li><a href="index.php">Home</a></li> <li><a href="about.php">About</a></li> <li><a href="contact.php">Contact</a></li> (Where I've put the end of the anchor, this forum is including [/url]) So obviously the list items would be added to the unordered list on each page, so I only have to update the navigation once for the whole site. So, if I'm on the home page, how do I use php to add a class to the Home list item? Hope that's all you need. Thanks! Quote Link to comment Share on other sites More sharing options...
Dilb Posted July 1, 2007 Author Share Posted July 1, 2007 Hi, sorry to 'bump' this, but I've not received a reply, and wondered if anyone could offer some advice? Obviously any help is very appreciated. Thanks. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 1, 2007 Share Posted July 1, 2007 If you pass along the page the current user is on to your navigation.html page (which you'll need to make a .php page) you can have a differant class on the current page. So you will include like: <?php include("http://www.yoursite.com/navigation.php?curr_page=about"); //whoops - forgot you'll need to use an absolute path otherwise you cant sent along get data ?> Then, in your navigation page: <?php $pages = array("home","about","contact");//put all the possible pages into an array - far easier $curr_page = $_GET['curr_page']; foreach($pages as $value){ echo "<li"; if($value == $curr_page){ echo " class='currentpage'"; } echo "><a href='".$value.".php'>$value</a></li>"; } ?> 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.