[PHP/HTML]Including a navbar with "active" element

Associate
Joined
21 May 2003
Posts
1,365
I don't know if this will be of any use to anyone, but I wrote it a while back and it's been handy on a few different projects. There are most likely better written examples on the net, but I thought i'd share it anyways.

Normally if you are highlighting the "active" navigation link for the current page (e.g. http://www.alistapart.com/articles/), you can't have the navigation menu as a seperate include because it needs to be slightly different for each page. (Having it as an include is handy because it means that you only need to edit one file should the navigation need updating).

Basically the code below allows you to have only one navigation list which you can include (using php) in your pages, whilst still retaining the "active" functionality.

Code:
<?php
	// set up the default navigation
	$nav1 = '<li><a href="addEnquiry.php">Add Enquiry</a></li>';
	$nav2 = '<li><a href="viewEnquiries.php">View/Edit Enquiries</a></li>';
	$nav3 = '<li><a href="createReport.php">Create Report</a></li>';
	$nav4 = '<li><a href="todaysCallbacks.php">Today\'s Callbacks</a></li>';
	
	// markup the correct list item as active - $thisPage is provided by the page which is including this file
	switch ($thisPage)
	{
		case 'ELTS :: Add Phone Enquiry':
			$nav1 = '<li><a href="#" id="current">Add Enquiry</a></li>';
		break;
		
		case 'ELTS :: View/Edit Phone Enquiries':
			$nav2 = '<li><a href="#" id="current">View/Edit Enquiries</a></li>';
		break;
				
		case 'ELTS :: Create Report':
			$nav3 = '<li><a href="#" id="current">Create Report</a></li>';
		break;
		
		case 'ELTS :: Today\'s Callbacks':
			$nav4 = '<li><a href="#" id="current">Today\'s Callbacks</a></li>';
		break;
	}

	// draw out the modified navbar with correct section active
	echo '<ul id="ELTSNav">';
		echo $nav1;
		echo $nav2;
		echo $nav3;
		echo $nav4;
	echo '</ul>';
	
?>

And then in each page you need to include the menu:

Code:
<?php
	// the name of the current page
	$thisPage = "ELTS :: Add Phone Enquiry";
	require_once('navBar.php');
?>

Feel free to point out any flaws or improvements.
 
robmiller said:
Code:
<?php

$nav = array(
    'Home' => '/',
    'About' => '/about',
    'Foo' => '/foo'
);

foreach($nav as $title => $link) {

    $current = ($link == substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '?')) ? 'id="current"' : '';

    echo '<li><a href="'.$link.'"'.$current.'>'.$title.'</a>';

}

is my preferred method :)

The above method only works if the page is requested with a GET method, i.e. there's a question mark in the url.

This adaption should work with any url:
Code:
$nav = array(
    		'Add Record' => 'addRecord.php',
    		'Search Records' => 'searchRecords.php'
		);

foreach($nav as $title => $link) 
{

	$current = ($link == substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/')+1, strlen($link))) ? ' id="current"' : '';

	echo '<li><a href="'.$link.'"'.$current.'>'.$title.'</a></li>' . "\n";

}
 
Ok, I get what you're saying, but from my understanding:

Code:
strrpos($_SERVER['REQUEST_URI'], '?')
gives the number of characters upto the first "?"

so
Code:
substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '?'))
gives the whole request string up until the first "?"

e.g. /news/20050401/article103.php or whatever.

so if you're comparing that against $link, then it's never going to match unless you put the full directory path in the array?

I may just not understand your directory structure.


Is there a less convoluted way of getting just say "addRecord.php" than using:
Code:
substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/')+1, strlen($link)))
bearing in mind that the file that calls the page may be an include?
 
Back
Top Bottom