Simple PHP Navbar
Wednesday, April 30, 2008
While working on a simple website that didn't need any kind of CMS with it, I came up with a very simple way of creating a dynamic navigation bar with PHP. The code is pretty simple, but I've always done this manually before and this just makes it simple. So here it is.
First we create a function in PHP:
function createNav($title, $link){
if("/".$link == $_SERVER['PHP_SELF']){
return "<li id=\"current\">".$title."</li>\r";
}else{
return "<li><a href=\"".$link."\">".$title."</a></li>\r";
}
}
And then this is how you call it:
echo createNav("Home","index.php");
So if you are on the page that navigation button relates to it will give you <li id="current">Home</li>. If you're on any other page, it gives you <li><a href="index.php">Home</a></li>.
Easy enough. So just call that for each button you need between your <ul> tags:
<ul>
<?php
echo createNav("Home","index.php");
echo createNav("About","about.php");
echo createNav("Products","products.php");
echo createNav("Contact","contact.php");
?>
</ul>
I know it's nothing spectacular, but it sped up the coding process for me once I figured it out, and it will certainly be a lot faster in the future if anything needs to be changed or added.
Comments
Ok, I just wanted to make sure I knew this. echo and createNav is the function, and “Home” and “index.php” are the strings?
Yep. The createNav() function returns either one thing or the other, so we just echo out what it returns back to us.
Were this really comes in handy is if you use this in a file by itself and just include it on all your pages. This way your nav is just in one place if you add a new page or something.
Chris,
I can’t seem to get php to return ‘#current’ to the current page. Any suggestions?
Joe.
I’m not sure what you mean Joe, your example link seems to be working fine when I looked at it.
It’s not supposed to return “#current”, it adds an ID to the current item and then you style it with CSS so it looks different. But like I said, your example seems to be working just fine.
Thanks for your help Chris. Shortly after I asked I figured out that $_SERVER[‘PHP_SELF’] only gives the directory instead of the entire URL. That’s what was messing it up.
Thanks again!
PS: know of a good PHP XML tutorial?
I have a sinking feeling that I’m going to be spending most of the seven and a half hours I’m paid to sit in this cubicle sifting through your articles…
Anyways, I figure I may as well mention the following, despite the fact that this post is almost two years old. Even if you don’t benefit from it, someone else might.
I noticed that in your PHP, you’re wrapping all your strings with double quotes. At the same time, whenever outputting variables, you are breaking out of the double quotes.
Double quotes have a major advantage - any simple variables (as far as I know, no arrays or objects) are parsed even when included in double quotes.
$name = “Fred”;
echo “My name is $name”; //My name is Fred
On the downside, if I remember correctly, doublequotes also take a little bit longer to process (negligible, I’m sure, but worth mentioning). The other option is to use single quotes, which process faster, but do not parse anything inside of them (not even \n or \r).
This, of course, has the benefit of letting you use double quotes inside your string without having to escape them.
echo ‘<li id=“current”>’;