Forum Moderators: coopster

Message Too Old, No Replies

Highlighting active link using PHP includes

How do you highlight where a user is on a navigation include?

         

mr_nabo

8:31 am on May 18, 2007 (gmt 0)

10+ Year Member



Hi,

I've been trying to figure out how to give a css class to an active link in a php navigation include. For example, if you're on the 'about' page:

<!-- nav.inc.php -->
<ul>
<li>Home</li>
<li class="active">About</li>
<li>Contact</li>
</ul>

However, if you visit the 'Contact' page, then that class changes to <li class="active">Contact</li>.

Is there a simple solution to this? The only idea that comes to mind is to add some sort of php 'if' statement using GET, such as:

<li <?php if($GET['about.php'] = TRUE) {echo 'class=\"active\"';}?> >About</li>

Any advice appreciated

dreamcatcher

8:38 am on May 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi mr_nabo,

I guess you could do something like this:

<ul>
<li>Home</li>
<li <?php echo (basename($_SERVER['SCRIPT_FILENAME'])=='about.php'? 'class="active' : '');?>">About</li>
<li <?php echo (basename($_SERVER['SCRIPT_FILENAME'])=='contact.php'? 'class="active' : '');?>>Contact</li>
</ul>

dc

mr_nabo

8:46 am on May 18, 2007 (gmt 0)

10+ Year Member



Hi dreamcatcher,

Fastest response I've ever had and it solved it straight away.

Thank you very much for that.

:)

dreamcatcher

11:30 am on May 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You`re welcome. :)