Page 1 of 1

PHP Navigation help

Posted: Mon Jun 01, 2009 5:09 am
by napalm
Hi All,
I have a site that consists of 24 pages.
Instead of adding a menu to every page, i plan of just including a file that contains the menu called 'nav.php'.

However the menu is a horizontal drop down menu, and my issue is that i want to know if there's a way that if you visit the child links
of the navigation, the parent would be 'selected' using CSS.

So for instance i have this menu in my 'nav.php' file:

<ul is "nav">
<li><a href="#" class="home">Home</a>
<ul>
<li><a href="#">page 1</a></li>
<li><a href="#">page 2</a></li>
</ul>
</li>

<li><a href="#" class="products">Products</span></a>
<ul>
<li><a href="#">Page 3</a></li>
<li><a href="#">Page 4</a></li>
<li><a href="#">Page 5</a></li>
</ul>
</li>
</ul>



So if you were to visit 'Page 1' the parent link 'Home' would be 'selected'.

Usually, If i was to actually put this menu on the home page i would just add :


<ul is "nav">
<li><a href="#" class="home selected">Home</a>
<ul>
<li><a href="#">page 1</a></li>
<li><a href="#">page 2</a></li>
</ul>
</li>
...

with this CSS :

#nav li .home.selected{
color: #ff1200;
}


But I'm wondering if there is a way using PHP that i can insert the navigation and sort through and basically say
"If this is Page 1, make HOME selected".

Any help is appreciated.
Thanks in advance

Re: PHP Navigation help

Posted: Mon Jun 01, 2009 3:42 pm
by Reviresco
First add something like this to the page:

Code: Select all

 
$this_page = 'home';
 
Then in your links, something like this:

Code: Select all

 
<li><a href="#" class="home<?php if($this_page == 'home') { echo ' selected';}?>">Home</a></li>
 

Re: PHP Navigation help

Posted: Mon Jun 01, 2009 5:17 pm
by napalm
Thanks heaps.