PHP Menus are Too Easy…At Last!

Posted July 14, 2011 by Kathy Davie in

Finally, finally, I got the PHP thing figured out for menus. I’ve really been in love with the idea of a single external menu file that I can edit, add, or generally “decorate” without having to change 3,056 pages as well. Sigh…the php menu…lust, desire, covet…

Well, with the help of my SMEs, we got it figured out. And it was incredibly simple. It makes me wonder about the guy last fall with whom I corresponded for weeks as he attempted to “help me” implement his easy method for creating this easy menu.

Starting the PHP Menu Process

The Menu

Cut-and-paste your entire menu into a new file saving it with a PHP extension—menu.php.

In the <head>, be sure to include the links to any external stylesheets such as the primary stylesheet that sets out the declarations for the menu! If you have separate stylesheets for the actual dropdown menu, include those as well. Be sure to hard code the full address for your stylesheets in your new menu file!

Include any menu hacks for Internet Explorer, etc. and the external links for any JavaScript, etc.

The HTML Page

In each web page that will use this menu, type in the DIV tag for the menu placement and add <?php include("menuFileName.php"); ?> inside the DIV.

<div id="colMenu"><?php include("menu.php"); ?></div>

Now save each page [using the php include statement] with a PHP extension—index.php instead of index.html, about.php instead of about.html, etc. (You may find you need to save every single page in your website with a PHP extension, see “The Kickers” below).

Upload all your pages including the new menu.php.

The Kickers

Every single page in your site that has an HTML file extension must be changed to a PHP. IF you want people to find the pages they have previously bookmarked.

The simplest way to do this is to create a redirect page. To do this you need a .htaccess file. The problem is that once you have this .htaccess file with its redirect code, it will redirect anyone trying to find a page bookmarked with its HTML extension to its PHP version. Which, if it doesn’t exist, will result in a 404.

This redirect does the same thing for any page in your new PHP site that has an HTML extension. I tried to leave some TOC links in a couple of my tutorials as HTMLs. Nuh-uh…I hads ta change ever’ting.

.htaccess

RedirectMatch permanent ^(.*).htm(l?)$ $1.php

Copy and paste the above code into a file you create and name “.htaccess”.

Upload the .htaccess file.

The Fun Part

I was showing my new menu setup to some of the guys and they just weren’t getting my excitement. So I added a new link— Happy Days! —in menu.php. Then I uploaded the menu file and had them check out the menus on any page they wanted. They’ve all decided they want to convert too.

I’m thinkin’ I just might convert my tutorial TOC menus too…


Leave a Reply