[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...


Comments
Kathy Davie   Monday, November 17, 2008 16:13
 
Okay, I am a newbie to all this. I tried inserting the HTML code into my footer navigation for a test page I'm working on. I then pasted in the php code into the bottom of my test page changing the "echo" line to reflect the test page's address. I then created .php pages for those links in the footer nav (changing their footer nav links and adding the php code as well). After uploading the four pages, nothing happens.
tedd   Tuesday, November 18, 2008 05:10
 
Kathy:

When you say nothing happens, what's that, a blank page?

If so, then check all your code. A misplaced quote could cause the script to fail and produce a blank page.

Cheers,

tedd
matthew   Saturday, January 30, 2010 17:43
 
I am relatively new to PHP, and am working on a sub-site for my school (it has not been uploaded to the school's online server yet, though). I have two questions:
1) Where specifically am I to put the helper code from part 2? In the head or body of the code?
2) I am using your "horizontal drop-down menu" ("http://sperling.com/examples/menuh/"). The Menu is located in a separate file using an (I think) client-side include made written in JavaScript ("http://www.boutell.com/newfaq.20050805/creating/include.html").
Before I added your smart menu code, everything worked great. But when I started to change the links using your
matthew   Saturday, January 30, 2010 17:49
 
above code, the drop down menu changed to display the text with a see through background. Do I need to change the CSS you included in your menuh example, or is there something else I am missing?

Thanks!!!
tedd   Thursday, February 18, 2010 08:04
 
matthew:

Answer to your first question,namely: 1) Where specifically am I to put the helper code from part 2? In the head or body of the code?

You may put it anywhere you want. I recommend that you place it at the bottom of the page, as I recommended in the instructions. You see, it doesn't make any difference where the code is located because the code is run and html is created before the browser see's anything.

Answer to your second question is a bit more complicated because you are using a "server-side" javascript include, which is not necessary. If you want to include something, just include it this way:



If you want to learn to learn something that will rock your web-design world, then learn how to use php includes -- as demonstrated here:

http://sperling.com/examples/include-demo/

If you learn that, you'll never need another javascript routine to do something that should have been done pre-browser. Don't get me wrong javascript has it's place, but it's place is client-side and if you can do something server-side, then do it there. That practice is better than expecting the user to have javascript always turned on.

Cheers,

tedd

"Post a Comment" has been deactivated.

I'm sorry, but there is just too much spam to review to provide answers. For example, for every honest question, there are over 1000 spams. I am leaving the comments here so that they may help others.

I find my time better spent providing more examples and attending to clients.

tedd