Discuss: Keeping Navigation Current With PHP
by Jason Pearce
- Editorial Comments
2 why not use PHP_SELF?
to the guy above me, try using a function like PHP_SELF (not sure what it would be called in cold fusion). then just use that to define your navigation…
posted at 06:41 am on November 7, 2003 by matt
3 nice, but
not too interesting for me, since I’m already using exactly this technique.
posted at 06:50 am on November 7, 2003 by tim
4 Alot of code
Why not simply have all your links in an array, loop through them while checking to see if the current iteration of the loop equals the current page as determined from PHP_SELF?
posted at 06:58 am on November 7, 2003 by Chase
5 nice intro to php but
you could have easily taken things a step further and used a loop to create the list. I would suggest something like this:
<?php
$navvars = array(
‘home’ => array(“Home”, “./”, “My Homepage”),
‘blog’ => array(“Blog”, “./blog.html”, “My personal Weblog”),
‘VALUES ASSIGNED TO $thispage’ => array(“ENTER LABEL HERE”, “LINK GOES HERE”, “DECRIPTION FOR TITLE TAG GOES HERE”),
‘about’ => array(“About Us”, “./about.html”, “Who we are and how we can be contacted”),
);
foreach($navvars as $page => $values){
print “ <li”;
if ($page == $thispage){
print ‘ class=“active”’;
}
print ‘>[url=”’.$values1.’” title=”’.$values2.’”]’.$values0.”[/url]</li>\n”;
}
}
?>
I actual use a slight variation of this.
posted at 07:08 am on November 7, 2003 by waylan
6 Handy tip for menus too
This is close to something I’ve just coded myself. My root pages now work by including a header, footer and menu. I use a variable to give the page name first (eg: “Index”) then use this to give a proper title tag. But that’s been covered above. Yet here’s my tip: use PHP to also generate a page-specific menu.
I realise this too is close to the article, but here’s my version. Depending on the page you’re on, you get a menu with links to the other pages – but NOT the current page. (Since you’re already on it.)
If you’re on the “Index” page, there’s no point displaying the “Home” link. Whilst if you’re on the “Articles” page, there’s no need to add a menu link to “Articles”.
So I simply use the page title already defined at the top of my file, and output a menu using simple PHP like this:
<?php
if ($title <> “Index”) {echo “\n<li><a href=\“index.php\” class=\“menu\”>HOME</a></li>”;}
if ($title <> “Articles”) {echo “\n<li><a href=\“articles.php\” class=\“menu\”>ARTICLES</a></li>”;}
if ($title <> “Artwork”) {echo “\n<li><a href=\“artwork/artwork.html\” class=\“menu\”>ARTWORK</a></li>”;}
if ($title <> “Photography”) {echo “\n<li><a href=\“photography/photography.html\” class=\“menu\”>PHOTOGRAPHY</a></li>”;}
if ($title <> “Links”) {echo “\n<li><a href=\“links/links.html\” class=\“menu\”>LINKS</a></li>”;}
if ($title <> “Tips”) {echo “\n<li><a href=\“tips/tips.php\” class=\“menu\”>TIPS & DEMOS</a></li>”;}
?>
Works a treat!
posted at 07:22 am on November 7, 2003 by Chris Hester
7 Re: Handy tip for menus too
I disagree, Chris. From a user interface point-of-view there is a very good reason to display a non-clickable “Home” menu item even while viewing the Index page, or a non-clickable “Articles” item while viewing the Articles page.
The code you posted results in a very confusing menu — as soon as I click on “Articles”, it disappears and is replaced by “Artwork”.
I use similar code to what you posted, but instead of not displaying the menu item at all for the current page, it displays it but without the A element so it is obvious which is the current page.
posted at 07:59 am on November 7, 2003 by Chris Burkhardt
8 Good Article, Nice Implementation
This is the same technique we use at http://automaticlabs.com – very useful and handy, and the perfect step to make CSS navigation “complete.”
posted at 08:27 am on November 7, 2003 by Dan Benjamin
9 $REQUEST_URI
I tend to use the reserved variable $REQUEST_URI for things like this. That way I don’t have to fool with declaring a variable on every page.
posted at 08:57 am on November 7, 2003 by Scott
10 I use CSS selector
Another way that I think is simpler and can also be use for other thing is simply to declare an ID on the body and use it as descendant selector.
This is a simplified example :
<body id=“intro”>
<ul id=‘mainmenu’>
<li class=“menuintro”>Intro</li>
<li class=“menucurriculum”>Curriculum</li>
<li class=“menuportfolio”>Portfolio</li>
<li class=“menuprojets”>Projets</li>
<li class=“menucontact”>Contact</li>
</ul>
#intro .menuintro { background-color: #000 }
It’s not with PHP but it works.
posted at 09:07 am on November 7, 2003 by Stéphane Curzi
Discussion Closed
New comments are not being accepted, but you are welcome to explore what people said before we closed the door.
Got something to say?
Discuss this article. We reserve the right to delete flames, trolls, and wood nymphs.
Create a new account or sign in below if you’d like to leave a comment.
Subscribe to this article's comments: RSS (what’s this?)



1 But what about pages generated from CF templates?
I have been waiting for a decent tutorial explaining how to get the current page nav change. Well done. But my site pages are generated through a Cold Fusion template. Placing $thisPage in the generating template would show the same current page in the nav throughout the site. So close…my search continues for an option for Cold Fusion.
posted at 06:39 am on November 7, 2003 by Blurred line