One of the things I love most about server side scripting is the little snippets of code you can write that are not necessarily big applications like phpMyAdmin but just very small 5 or 6 liners but which nonetheless make your life ten times easier.
One of the simplest little snippets I wrote allows you to call a script in via an include which displays sub menu items for a specific menu item depending on which menu you’re viewing.
Basically the way it works is like this:
First you build the structure of your site on the server:
Now, on every single one of those php pages we call the script ‘menu_by_directory.php’ in via an include (note that this script is in its own subdirectory called ‘inc’. I always put included files in their own subdirectory for easier housekeeping).
So ‘menu_by_directory.php’ is a script that does all the work. It orients itself in the structure of the site and then displays the correct submenu for the area of the site its been included into. Here’s how:
On the first line we set the root directory of our website. We then use dirname($_SERVER['PHP_SELF']) to grab a load of information about the directory the script currently resides in. because this info is returned as an array object we need to iterate through it to get the one thing we need – the current directory name which we finally assign to $TheDIR.
Then we use this to do a simple comparison using case selects (case selects run faster and are less server intensive than if…else statements):
All we do here then is say: ‘If $TheDIR holds the value ‘about’ then use the first menu. If it carries the value ‘portfolio’ then use the second. If it carries neither of these then use the third.
As part of that script, in order to build the links we use the $root and $TheDIR values to write out the link targets – this makes updating easy if we change the site location/domain at some point.
Now, there’s nothing earth shattering here but its things like this – little bits of code – that I love. Very unobtrusive, a massive labour saver and easy to understand and implement time and time again.
Here’s a working example.
oh, sure, easy for YOU!
🙂
Interesting. For sites that don’t use a CMS we do something similar, which involves defining a variable for each page. It also allows highlighting the current page, by giving an ID to the link which we can target in the CSS etc. I’m sure that can be done better this way, but I suck at PHP 😛
Very good. I do a fair amount of PHP work – mainly in relation to MySQL though, and so I’d not thought of it for something like this. I will now though … thanks very much. David.
If you are the sort of person that names pages properly, you could do away with the switch() statement and just have the PHP script grab the filenames within the relevant directory, then loop through them and convert them to proper case for the link text.
That way if you add or delete files from a particular directory, you don’t have to go back and edit your menu script.
Your time offset is out by an hour, btw.
@Matt: good idea about getting filenames and yeah, the clock is out. I’ll get around to fixing it one day ;o)
Glad everyone else got something out of it – esp you AD ;o)
I am having a little problems with this. For some reason $TheDir is coming up as the directory that “menu_by_directory.php” is in and not the directory that the page is in. Any ideas how to fix this?