[thelist] PHP - good ways to indicate current page in navigation?

Tony Crockford tonyc at boldfish.co.uk
Tue Jul 18 00:11:45 CDT 2006


Tom Dell'Aringa wrote:
> Hi folks,
> 
> It's been awhile since I've done any PHP. Currently I am doing a simple web
> site that has some horizontal navigation. I'd like to indicate what section
> the user is in by writing out a class on a LI item dynamically server side.
> So for example, I might have 2 categories, home and services. Services is a
> drop down with 4 items under it. If the user clicks on services, or any of
> the subpages, I want to write out that class on the services LI so I can
> indicate to the user they are in the services section.
> 
> The hacked up way I have done it in the past on simple sites was to do
> something like this:
> 
> if($pageName == "home") { ?> class="activePage" <?php } ?>
> 
> where $pageName was set locally in each page. This is lame of course, and
> hardly scales well - nor does it take into account pages within a
> section/directory. Can anyone point me to some examples along this line?

Hi Tom,

what I do is to use PHP to put an ID or class on on the body tag and 
then marry up the body id with an id on the nav li in CSS so it 
becomes active.

e.g.

#contact li#navcontact a,
#about li#navabout a,
#home li#navhome a{
     color: red;
}

<body id="home">

<ul>
     <li id="navcontact"><a href="contact.htm">contact us</a></li>
     <li id="navabout"><a href="about.htm">about us</a></li>
     <li id="navhome"><a href="index.htm">home</a></li>
<ul>


will have the home link in red...

<body id="about">

<ul>
     <li id="navcontact"><a href="contact.htm">contact us</a></li>
     <li id="navabout"><a href="about.htm">about us</a></li>
     <li id="navhome"><a href="index.htm">home</a></li>
<ul>

will have the about link in red...

etc.


that way the list stays the same, just the body id changes...


more live use here:

http://www.bclm.co.uk
(built with PHP into static files)

hth



-- 
Join me: http://wiki.workalone.co.uk/
Thank me: http://www.amazon.co.uk/gp/registry/1VK42TQL7VD2F
Engage me: http://www.boldfish.co.uk/portfolio/




More information about the thelist mailing list