May 6, 200917 yr Hi Guys, This is such a newbie question I am sure! Basically I am getting more and more into php and I have started to use php includes to add my navigation to pages. This is all working really well and it is saving me loads of time However, how do you add an active class to your links if you use the same navigation file for every page? For example... I used to use <li><a class="main" href="#">Home</a></li> <li><a class="selected" href="#">About</a></li> <li><a class="main" href="#">Contact</a></li> and then change it for each page... But how do I achieve the same effect so I can show the user which page they are on with a highlighted button using a php include?!? Cheers in advance to the hero who tells me how to do it
May 6, 200917 yr There are a few methods to do this, but this is what i do. Include this at the very top of your page (obv change the page name for whichever page it is) <?php $page = "contact"; ?> And then i have my nav like this <ul> <li><a href="/" class="title" title="Homepage">Home</a></li> <li<?php if ( $page == 'about' ) {echo ' class="selected"';} ?>><a href="about/" title="About">About</a></li> <li<?php if ( $page == 'portfolio' ) {echo ' class="selected"';} ?>><a href="portfolio/" title="Portfolio">Portfolio</a></li> <li<?php if ( $page == 'contact' ) {echo ' class="selected"';} ?>><a href="contact/" title="Contact">Contact</a></li> </ul> Then in your css, you just need to style the 'selected' class. But as i said, there's a few methods, some people use an id attribute on the body tag (i'll dig up an article on that) but that's my proffered way to do it. EDIT: WDF wrapped the code, so it looks a little difficult, c/p into a text editor and just put the <li>'s on a single line, and it should make a little more sense.
May 6, 200917 yr oooh, looks good, i'ma take ElanMan's approach i think from now on, seems easier Never used strpos before but thanks, that's my one thing learnt for today (:
May 6, 200917 yr was just writing something exactly as your looking for when I took a break to have a browse here! The code im writing is maybe a little more advancedx than your looking for but might help in the future while($aResults = mysql_fetch_array($aQuery)) { $vPageURL = ($aResults['page_id'] > 1)? "/rsm/{$aResults['page_id']}/{$aResults['page_url']}" : "/"; echo "<a href=\"{$vPageURL}\"".((RICE_PAGE_MENU_PARENT_ID == $aResults['page_menu_id'])? " style=\"color:#F7C317\"" : '').">".((strlen($aResults['page_menu_text']) > 0)? funcHtmlSpecialChars($aResults['page_menu_text']) : funcHtmlSpecialChars($aResults['page_title']))."</a>\n"; } Theres a ternary operator in there that decides if its the current page of if we are in the parent page! (Multi level menuing bah)
Create an account or sign in to comment