Home arrow PHP arrow 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.

  1. Dynamic Generation of Menu Structures and JavaScript Rollovers in PHP
  2. Making a Template
  3. Starting to Automate
  4. The power of repetition
  5. Ordering up a Menu
  6. Adding Pages
By: Chris Mospaw
Rating: starstarstarstarstar / 11
May 10, 1999

print this article


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.

>>> More PHP Articles          >>> More By Chris Mospaw

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: