A LIST Apart: For People Who Make Websites

No. 162

Discuss: Keeping Navigation Current With PHP

Pages

 <  1 2 3 4 5 >  Last »

21 I prefer a little simpler approach

I agree with some of the other comments regarding the unneeded complexity introduced by the PHP as used. My approach is to give each link in the navigation menu its own ID. I do use PHP to include it on each page of the site.

Then, rather than use PHP to test if I’m on the current page, I add page-specific styles (knowing what page I’m on already…just like $thispage does), and add them (may be more than 1; for example a level 1 and a level 2 navigation highlight) to the header in the <style> section. Here again, I use PHP to include the styles so that I can change the type of styling for each “you are here” element by changing the included style.

Here’s an example that I used on http://www.princetonacademy.org.

<style type=“text/css”><!—

a#the-school { <?php include(“style-includes/level1-youarehere.inc”); ?> }

a#allboys-why { <?php include(“style-includes/level2-youarehere.inc”); ?> }

—></style>

So, my level1 and level2 “you are here” styles are easily managed sitewide.

I know beforehand the ids of the nav menus (also included) and they never change, so this approach is simple. It does take a tad more bandwidth due to the ids for each link, as opposed to a little more processing power on the server side for your “if $thispage” approach.

posted at 03:21 pm on November 7, 2003 by Bob Monsour

22 my twist

i’m using something similar to this with my personal site right now. my code actually has an array of all of the sections of the site with their corresponding filenames plus the navigation html in a php include file. the current page is not determined by manually setting a variable, rather, i use something like this:
$current = basename($_SERVER[‘PHP_SELF’],”.php”);
of course that only works if all of the filenames of the site are in the form sectionname.php.
but it’s nice in the sense that one include file is used for every single page to generate the navigation, highlight the current section, etc, and i don’t have to change anything at all from page to page.

posted at 03:26 pm on November 7, 2003 by vlad

23 Maybe some improvements

I think that the all thing is in there, altough the code is very confuse for some people, i guess.

Someting like:

<?php $thispageAR[“menu1”] = ($thisPage==“Page One”): “id=\“currentpage\”“ ? “”; $thispageAR[“menu2”] = ($thisPage==“Page Two”): “id=\“currentpage\”“ ? “”;
// for all links on menu and then <li <?=$thispageAR[“menu1”];?>>About Us</li> <li <?=$thispageAR[“menu2”];?>>Work</li>
// …
?>

will do a major clean on your code

posted at 06:04 pm on November 7, 2003 by Jose Silva

24 My Version

One thing that doing it by the body id method doesn’t give you is removing the link to the current page (as is recommended) — you either have to have that link to the current page, or insert the menu code seperately on each and every page, making it a nightmare to change.

I (already) use similar code on my site — not really intended for updating by a non-coder, though I don’t think it’s /too/ bad: (the spans are for the css)

<ul id=“menu”> <?php $menu=array( array(‘url’ => ‘/’, ‘name’ => ‘Home’), array(‘url’ => ‘/fiction/’, ‘name’ => ‘Fiction’), array(‘url’ => ‘/museblog/’, ‘name’ => ‘Muse Blog’), array(‘url’ => ‘/maps/’, ‘name’ => ‘Maps’), array(‘url’ => ‘/art/’, ‘name’ => ‘Artwork’), array(‘url’ => ‘/links/’, ‘name’ => ‘Links’), array(‘url’ => ‘/search/’, ‘name’ => ‘Search’)); while (list ($key, $item) = each ($menu)) { echo ‘ <li>[url=”’ . $item[‘url’] . ‘“id=“this“id=“open”]<span>’ . $item[‘name’] . “</span>[/url]</li>\n”; } ?> </ul>

and on the begining of a page
I have something like:

<?php
$this=’/fiction/’; #$open=’/fiction/’;

or

<?php #$this=’/fiction/’;
$open=’/fiction/’;

depending if the page is the one linked to by the navigation item or a page under it.

posted at 07:22 pm on November 7, 2003 by Crys

25 Inventive

While I am not a programmer of sorts, I can truly appreciate the creativity and inventiveness of this solution to an old problem. Well done and Thank You!

posted at 08:00 pm on November 7, 2003 by Jason Bailey

26 Coldfusion solution

A Coldfusion version of this is simple. Here it is (a copy of the article’s code in CF).

set page variable at top of page:

<cfset page=“About Us”>

included navigation (<cfinclude template=“navigation.cfm”>):

<div id=“navigation”> <ul> <li <cfif page IS ‘Home’>id=“currentpage”</cfif>>[url=”#”]Page One[/url]</li> <li <cfif page IS ‘About Us’>id=“currentpage”</cfif>>[url=”#”]Page Two[/url]</li> <li <cfif page IS ‘Products’>id=“currentpage”</cfif>>[url=”#”]Page Three[/url]</li> <li <cfif page IS ‘Contact’>id=“currentpage”</cfif>>[url=”#”]Page Four[/url]</li> </ul>
</div>

A more graceful way, assuming you’ve organized each section into its own folder, would be to parse out the URL. If the folder name following the URL is /products/ then set the appropriate id, and so on. This would save you from setting a variable on every page…which is just as much a pain in the backside as remembering to set the id on every page. In addition, it would be a lot easier to manage assuming you’ve placed your navigation file as an include.

posted at 08:07 pm on November 7, 2003 by Mike Hazard

27 CSS only +1

The “CSS only” solution is more elegant, efficient, scalable and “cheaper” :)

posted at 08:12 pm on November 7, 2003 by ssn

28 a speedup

my understanding is that require() is faster than include().

posted at 01:24 am on November 8, 2003 by Paul Schreiber

29 Making it simpler for a newbye

The purpose of this article seems to introduct a php tecnique to people that never used it. Hence i would suggest a couple of things that could get the newbye’s reading and understanding experience simpler. I agree though with this per-link or per-line use of the if() statement to leave it easy to understand (instead of above suggested hashes and arrays).
In the “HTML and PHP code for navigation.php” you are echo’ing with double quotes, that are not actually needed. I think that
<?php if ($thisPage==“Page Three”) echo ‘ id=“currentpage”’; ?>
could result more readable and understandable by a newbye.
A newbye also wouldn’t know (s)he has to rename also the including page from “.html” to “.php” for apache to parse.
Best regards.
flevour

posted at 02:00 am on November 8, 2003 by Francesco Levorato

30 good article with good comments

I too prefer to use $_SERVER[‘REQUEST_URI’]; rather than hand coding the page title for each page (isn’t that the point of server-side coding?), however some of the comments here have given me some good ideas about better using CSS rather than relying on ‘echo’ing out each list item.

While the majority of comments on this article may seem critical to some degree – it has brought forward rewarding discussion.

posted at 04:22 am on November 8, 2003 by Mike

Pages

 <  1 2 3 4 5 >  Last »

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.

Remember me

Forgot your password?

Subscribe to this article's comments: RSS (what’s this?)