HomePHP Page 3 - Dynamic Generation of Menu Structures and JavaScript Rollovers in PHP
Starting to Automate - PHP
Did you ever have one of those sites that started out small, but kept growing and growing? Adding new pages to a site or removing old ones can often take more time than developing the pages themselves.
The first step in automating the generation of these pages is to determine which page is being displayed. PHP comes with great environmental variable support, so determining this information is fairly straightforward.
I've found that certain environmental variables are not uniformly supported on all implementations of PHP and all servers. Since my local development machine is a Windows 95 box and the Internet server a Unix box, the code that works on both is a bit convoluted. But it works on all installations I've tested it on.
This code, which should be put at the top of the script, simply puts the name of the current page (with no file type suffix) into the $page variable:
You will probably have to adjust the "4" in line 3, and
the "3" in line 4 since they refer to the depth of the directories on your machine, and that's likely to be different than mine. Inserting an echo "$page n"; between each line in the above snippet helps to track where things are going while adjusting lines 3 and 4 to work properly in your configuration.
Now that we have the current page name without any file extensions, we're ready to start having PHP automate some items, such as the header graphic. Replace the "1 - Header Area" text with this line:
Notice the "inline" PHP in the SRC attribute, which just
echoes the page name followed by the "-h" suffix. The height and width attributes are "hard wired" in, but can be changed at any time if the dimensions of the graphics change.
Since this site will have more than one page, the script will have to know what the other pages in the site are named. For ease of maintenance, do this in a separate script called PAGES.HTML that is INCLUDEd in the main script. All PAGES.HTML does it to define an array containing the names of all the pages to be dynamically generated.