scriptygoddess

15 May, 2002

Smart site nav

Posted by: kristine In: Bookmarks

Brenna of mngeo.com (and an all-around smart person on the MT boards!) posted really nice tutorial on how to make your site navigation know what page you are on; even when you are using a fixed header.

php site nav, using if/else and $REQUEST_URI

4 Responses to "Smart site nav"

1 | Lynda

May 16th, 2002 at 8:41 am

Avatar

Jennifer also posted something similar on the site not too long ago that will work in either PHP or Javascript.

Always good to have more options!

2 | kristine

May 16th, 2002 at 1:22 pm

Avatar

Yeah, I thought it looked familiar to something over here, but I didn't go looking like I should have! Thanks for posting the link :)

Still, like you said, its good to have options, and it was a well-written tutorial, so I wanted to share :)

3 | Jade

May 18th, 2002 at 4:21 pm

Avatar

My way was slightly different. Here's my navigation file:

<?php

# set the info for the navi stuff. first part of each sub-array is the plain link, second is the active link.

$nav["about"] = array("<p><a href=\"/about\">about</a></p>", "<p class=\"active\"><a href=\"/about\">about</a></p>");

$nav["links"] = array("<p><a href=\"/links\">links</a></p>", "<p class=\"active\"><a href=\"/links\">links</a></p>");

$nav["weblog"] = array("<p><a href=\"/weblog\">weblog</a></p>", "<p class=\"active\"><a href=\"/weblog\">weblog</a></p>");

$nav["journal"] = array("<p><a href=\"/journal\">journal</a></p>", "<p class=\"active\"><a href=\"/journal\">journal</a></p>");

$nav["contact"] = array("<p><a href=\"/contact\">contact</a></p>", "<p class=\"active\"><a href=\"/contact\">contact</a></p>");

$nav["webcam"] = array("<p><a href=\"/webcam\">webcams</a></p>", "<p class=\"active\"><a href=\"/webcam\">webcams</a></p>");

$nav["phlog"] = array("<p><a href=\"/otw\">phlog</a></p>", "<p class=\"active\"><a href=\"/otw\">phlog</a></p>");

$nav["features"] = array("<p><a href=\"/features\">features</a></p>", "<p class=\"active\"><a href=\"/features\">features</a></p>");

$active = getenv(QUERY_STRING);

# for each $x in $nav check if $x is the same as the query string and if it is use $x[1] otherwise use $x[0]

foreach ($nav as $bit => $link) {
if ($bit == $active) {
print $link[1];
print "\n";
} else {
print $link[0];
print "\n";
}

}

?>

So with the comments in there you can pretty much understand what it does, even if you weren't familiar with PHP.

Then I include this in each of my pages like this:

<?php
include('http://www.whisper-gallery.com/shared/menu.php?links');
?>

or..

include('http://www.whisper-gallery.com/shared/menu.php?weblog');

Just have the bit after the question mark match the name of whatever element of $nav in the navigation script applies to the section of the site that you are wanting to mark as active.

You need to include the navigation script using the full URL because PHP does not provide any function for including local scripts with information being passed to them. And believe me, I looked 😉

Initially I was worried about having to use the full URL to include the script rather than just a local path, but the friend who hosts me said it was ok and wouldn't really use much more in the way of server resources because a properly configured server should recognise that it's requesting a page from itself rather than over the internet and skip a few steps in the whole thing process of serving the file to itself. Or something. Sorry, I'm not so great with web servers just yet 😉

4 | Jennifer

May 21st, 2002 at 8:53 am

Avatar

Cool idea! Thanks Jade!

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements