[web-tips > smart-menu]

Smart Menu

One of the things that brother me in a web site is where you can navigate to a certain page and the navigation menu will permit you to click that page again causing an unnecessary reload of the page. Why would anyone want to reload that page again? If you want a refresh, then use the browser's refresh button.

So, if you want a self-aware menu -- something that knows where the visitor is and will not permit the visitor to reload the current page via the navigation -- then it's very simple to implement with php.

Simply do three things:

  • 1. Place the following navigation code (with your links) in your web pages.
										
<ul>
	<li><a <?php menu("index.php");?> > Home</a></li>
	<li><a <?php menu("web-tips.php");?> > Web Tips</a></li>
	<li><a <?php menu("freeware.php");?> > Freeware</a></li>
	<li><a <?php menu("books.php");?> > Books</a></li>
	<li><a <?php menu("resume.php");?> > Resume</a></li>
	<li><a <?php menu("contact.php");?> > Contact</a></li>
</ul>
  • 2. Place the following helper php code somewhere in your web page (i.e., bottom).
			
<?php
	function menu($a)
		{
		$u = basename($_SERVER['PHP_SELF']);
		if ($u==$a)
			{
			echo("");
			}
		else
			{
			echo("href=\"http://yourdomain.com/$a\"");
			}		
		
		}
?>

  • 3. Change your ".html" suffix to ".php".

What does this do?

The php suffix makes the php interpreter read the file and execute the code. Your web page will work the same as before except it will now process the embedded php code.

The code simply takes a php call ($_SERVER['PHP_SELF']) and compares it to the following string. If a match is made, then it shows just the title, and if it doesn't, then it makes it a link.

Please donate $5 below for my development effort -- thank you.

This technique W3C validates under both HTML 4.01 Strict and CSS guidlines.

Enjoy.

Tedd...