Dynamic Generation of Menu Structures and JavaScript Rollovers in PHP - Starting to Automate (
Page 3 of 6 )
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:
<?
$page=getenv(SCRIPT_NAME) ;
$page = split( "/", $page, 4);
$page = "." . $page[3];
$page = split( "\.", $page, 3);
$page = $page[1];
?>
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:
<IMG SRC="<? echo "$page-h.gif"; ?>" WIDTH=596 HEIGHT=50
BORDER=0 ALT=""><BR>
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.
<?
$site_pages = array("index", "info", "clients", "contact" );
?>
There would be 4 pages in the dynamically generated menus
on this particular site. Don't forget to insert:
<? include "pages.html"; ?>
near the top of the TEMPLATE.HTML
script.