Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Newbie question...

Featured Replies

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 :)

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.

oooh, looks good, i'ma take ElanMan's approach i think from now on, seems easier :p

 

Never used strpos before but thanks, that's my one thing learnt for today (:

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)

Woah. Actually that strpos() function is really handy... thanks!

  • Author

This is why i FRICKIN love this forum :)

 

Cheers guys - works a treat :)

 

Love it!!

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.