A LIST Apart: For People Who Make Websites

No. 162

Discuss: Keeping Navigation Current With PHP

Pages

 <  1 2 3 4 >  Last »

11 1

I too prefer CSS selectors for this – why use a server side technology when there’s a reliable client side alternative that doesn’t rely on scripting?

posted at 11:24 am on November 7, 2003 by Simon Willison

12 Yuck

Dear god that’s some seriously ugly PHP.

posted at 11:32 am on November 7, 2003 by .

13 Looks familiar

That looks close to what I did a several months ago, [url=“http://www.reames.org/comment.php?comBID=150”]www.reames.org, magic menus[/url]. From the comments it seems to be a common practice. I would like to see other sites pick up on it.

posted at 12:02 pm on November 7, 2003 by Randy

14 Go easy on the html-ers

A nice article, and a good idea, but horrible code for non-programmers. Perhaps throw it all in one echo tag. It’s not a purists route, but it’ll make it easier to read, and easier for the timid HTML’er to implement.

posted at 12:05 pm on November 7, 2003 by strudle

15 using parameters in the URL

While taking a UNI-course in LAMP and creating a totally bogus web-site, I modified the original a-list-apart technique to utilise the URL query-string to select between the ‘on’ and ‘off’ settings :
<?php if (isset($_GET[‘vis’])) {$vis = $_GET[‘vis’];} else {$vis = ‘’;} ?>

<div id=“tabs”> <ul> <li class=”<?php if($vis 'kommende') {echo('here');} else {echo('standard');} ?>"> [url="/arrangementer.php?vis=kommende;"]kommende[/url]</li> <li class="<?php if($vis ‘afholdte’) {echo(‘here’);} else {echo(‘standard’);} ?>”> [url=”/arrangementer.php?vis=afholdte;”]afholdte[/url]</li> </ul>
</div>

posted at 12:16 pm on November 7, 2003 by Bjarne D Mathiesen

16 This is ridiculous.

Right after you say that adding unique CLASSes IDs to list items can become cumbersome, you go and show us a MORE cumbersome way to do things. I’ll stick with my plain old lists, thank you.

posted at 01:43 pm on November 7, 2003 by Scott Johnson

17 A simpler approach

This is something I run into on almost every site I do. In theory you can accomplish this just with CSS3 selectors, but I want something that’s going to work well for a useful majority of people who visit my sites.

My approach is to have a normal list based menu like I always do in plain XHTML. Then read that menu into a variable (I use HEREDOC syntax) and iterate over the links to look if any of them match the REQUEST_URI. I match the first part of the link only because I want links deeper in the hierarchy to show the highlighted section. It’s not perfect code, but it works very well, is fast, and doesn’t require you manually building an array or some such. Just take your current menu code, wrap it in this, and maybe add a CSS rule.

Since this forum provides no good method for posting and formatting advanced content, I have made the code available at my site here: http://photomatt.net/scripts/intellimenu . Suggestions are welcome and the code is free for anyone to use, modify, or steal.

posted at 02:03 pm on November 7, 2003 by Matt

18 Use Smarty templates.

Forget about mixing php and html codes !! This is really bad.

Use a template engine instead. The best of the best : Smarty.

http://smarty.php.net

posted at 02:07 pm on November 7, 2003 by Dominique PERETTI

19 Doube vs. Single quotes

Useful article, but one little suggestion: use single quotes on the echos (might as well on the $thispage=… checks as well) – saves you from having to escape out each of those double quotes, and is ever so slightly less processor-intensive.

Of course, if you’re trying to echo a variable or PHP’s special characters (ie. \n \r etc.) in a string, you’ll have to use the double quotes. But in this example, it just sacrifices code readability.

posted at 02:10 pm on November 7, 2003 by Sean

20 I'm with Stéphane...

I prefer to id each list item and then append a “you are here” class if it’s the selected page.

<?php
if ($page) {
echo “<style type=\“text/css\”> #menu li#$page a { display: block; background-color: #ddd; font-weight: bold; }
</style>”;
} else {
echo “<style type=\“text/css\”> #menu li#home a { display: block; background-color: #ddd; font-weight: bold; }
</style>”;
}
?>

<ul> <li id=“home”>[url=“index.php”]home[/url]</li> <li id=“portfolio”>[url=“index.php?page=portfolio”]portfolio[/url]</li> <li id=“photos”>[url=“index.php?page=photos”]photos[/url]</li> <li id=“consulting”>[url=“index.php?page=consulting”]consulting[/url]</li> <li id=“about”>[url=“index.php?page=about”]about[/url]</li>
</ul>

posted at 02:41 pm on November 7, 2003 by Robin Martin

Pages

 <  1 2 3 4 >  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?)