Highlight active link

12 posts by 5 authors in: Forums > CMS Builder
Last Post: March 9, 2010   (RSS)

By benedict - August 24, 2009

Hi guys,

I have what hopefully is an problem. I wish for the active link to become highlighted when the visitor arrives at that page.

I have got this working in HTML no problem by adding a class called "active" to the <li>. You can see below:

<ul class="nav">
<li class="active"><a href="index.php" class="home">Home</a></li>
<li><a href="executive-recruitment.php?Who-Is-hunterPac-1" class="who">Who</a></li>
<li><a href="executive-recruitment.php?What-2" class="what">What</a></li>
<li><a href="executive-recruitment.php?Why-3" class="why">Why</a></li>
<li><a href="executive-recruitment.php?How-4" class="how">How</a></li>
<li><a href="contactus.php" class="contact">Contact</a></li>
</ul>


Now, who, what, why and how are all in a section called "Other Pages". How can I dynamically get the correct navigation point to highlight (i.e. have class of "active" added to the <li>) when the visitor reaches a page?

Thanks in advance.

B

Re: [benedict] Highlight active link

By Chris - August 25, 2009

Hi benedict,

If you're only worried about solving this for executive-recruitment.php, and you're going to hard-code links like that, one solution would be to test the current record's "num" field and insert some HTML if it matches on each link. For example:

<li<?php if ($record['num'] == 1) { echo ' class="active"'; } ?>><a href="executive-recruitment.php?Who-Is-hunterPac-1" class="who">Who</a></li>
<li<?php if ($record['num'] == 2) { echo ' class="active"'; } ?>><a href="executive-recruitment.php?What-2" class="what">What</a></li>
<li<?php if ($record['num'] == 3) { echo ' class="active"'; } ?>><a href="executive-recruitment.php?Why-3" class="why">Why</a></li>
<li<?php if ($record['num'] == 4) { echo ' class="active"'; } ?>><a href="executive-recruitment.php?How-4" class="how">How</a></li>


I hope this helps you out! Please let us know if you have any more questions or comments.
All the best,
Chris

Re: [chris] Highlight active link

Hi Chris,

Thanks for taking the time out to help me. This worked a treat. Thanks!

Cheers,

Benedict

Re: [benedict] Highlight active link

By Chris - March 3, 2010

Hi benedict,

Can you please attach the complete PHP source code for your page?
All the best,
Chris

Re: [chris] Highlight active link

By benedict - March 3, 2010

Here you go - courses.php is the file and sidenav.php is the include with the code I need to correct.

Re: [benedict] Highlight active link

By Donna - March 3, 2010

Hi Benedict -- nothing was attached to your post, can you give that another try?
Donna

--
support@interactivetools.com

Re: [Donna] Highlight active link

By benedict - March 3, 2010

Oops - here you go.
Attachments:

archive_002.zip 7K

Re: [benedict] Highlight active link

By Chris - March 4, 2010

Hi benedict,

So you have three subsets of records from your table, and you want to figure out which subset the currently selected record belongs to?

Assuming your currently selected record is $record, you can check if it's in a list (and if so, output " selected") like this:

<a href="#" class="opener<?php if (in_array($record['num'], array_pluck($courses_nationalRecords, 'num'))) { echo " selected" } ?>">

You can do this for each of your three lists, making sure to change the name of the list you're checking ($courses_nationalRecords above) for each.

I hope this helps. Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Highlight active link

By aev - March 5, 2010

Hi Chris,

Is this a shortcut for creating a foreach loop on the array, with an if statement checking for a match inside? If yes, it's great!

<a href="#" class="opener<?php if (in_array($record['num'], array_pluck($courses_nationalRecords, 'num'))) { echo " selected" } ?>">

-aev-