Dynamic Generation of Menu Structures and JavaScript Rollovers in PHP (
Page 1 of 6 )
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.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.
Even with advanced search and
replace, it can quickly become a real chore to manually change all the links on
every page, and a nightmare to keep up with things as the site grows to 20 or
even 30 pages. If you throw JavaScript rollovers into the mix, things can
quickly get out of hand.
Fortunately, PHP is perfectly suited for
creating menu structures and generating JavaScript for rollovers automatically.
I hate to do anything twice, especially coding, so I used these techniques when
developing my company website (http://insight.ourinternetsite.com). You might
want to take a look at the pages to get an idea of the effect we're
after.
All I need do to add a page to the site, and more importantly, a
link to that page on every other page as well as JavaScript rollovers, is to
generate a handful of graphics files from templates in PhotoShop, and plug the
text into the template file. PHP scripts take care of the rest, and maintenance
is a snap because changes are required on only one or two scripts rather than 30
or 40 pages.
This article is a step-by-step process, so you might want to
pull up your favorite HTML editor. It also assumes that you have a basic
familiarity with JavaScript and HTML table layout. The servers on all of my
sites are all set up to parse HTML files as PHP, so these examples are called
FOO.HTML, instead of FOO.PHP. You may have to rename the files based on your
server configuration.
There is a zip file available of an example of how
this works, complete with the necessary graphics files and all source code.
Download it here.
Starting Out
Whenever building a site of
any size, it's best to start out with a test page or two to make sure the basic
underlying structure is sound. Most pages seem to take the form of a large
table, with a menu structure about 100 pixels wide on the left or right, and a
"main body" area to take up the rest of the room.
Call me old-fashioned,
but I still develop for 640 pixel wide screens, which makes a 596-pixel-wide
table about the maximum, with cell spacing and cell padding set to 0. Typically,
the link buttons within the menu structure are about 100 pixels wide, justified
to either side of the cell. Diagram1 shows this basic layout that this code
(named TEMPLATE.HTML) produces:
<HTML>
<HEAD>
<TITLE>Template</TITLE>
</HEAD>
<BODY>
<TABLE WIDTH=596 BORDER="1" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD width=596 VALIGN="baseline" COLSPAN="2">
<P ALIGN="center">
<IMG SRC="blank.gif" WIDTH=596 HEIGHT=1 BORDER=0 ALT=""><BR>
1 - Header Area</P>
</TD>
</TR>
<TR>
<TD WIDTH="100" VALIGN="top">
<P ALIGN="center">
<IMG SRC="blank.gif" WIDTH=100 HEIGHT=1 BORDER=0 ALT=""><BR>
2 - Menu Area</P>
</TD>
<TD WIDTH="496" VALIGN="top">
<P ALIGN="center">
<IMG SRC="blank.gif" WIDTH=496 HEIGHT=1 BORDER=0 ALT=""><BR>
3 - Main Body</P>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>
The file BLANK.GIF is a 1x1 pixel clear cell, perfect for
fleshing out tables, and is necessary to make the page render the same in
Netscape and Explorer. BORDER is set to 1 for this example, but is usually left
at 0 on the final pages.
The principal areas of concentration are: 1, the
Header area; and 2, the Menu area; since 3, the Main Body, does not lend itself
to dynamic generation (unless you have database-generated sites, but that's
another topic).
The key to making all of this work is keeping the graphic
file names within a strict convention. It is easiest to base this naming
convention on the page name, using a suffix for graphics files to indicate which
component they are. For example, on a page named TEST.HTML the graphics file for
the header graphic would be named TEST-H.GIF (or TEST-H.JPG). We'll get into
more details in just a bit.
The only other limitations are that each type
of element (button, header, etc.) must be the same format (JPG, GIF, or PNG) and
they should all be the same pixel dimensions. In other words, if your header is
a 400 x 60 pixel JPG file, all of your headers must also be 400 x 60 pixel JPG
files; while if your buttons are 92 x 15 pixel GIF files, they all must be 92 x
15 pixel GIF files. They can be any pixel depth, any byte size, etc. This can
all be changed at any point. The key is consistency.
(NOTE: If you don't
care about generating proper HTML with HEIGHT and WIDTH tags in the <IMG>
tag, you don't have to keep all items of a type the same pixel dimensions, but
your pages risk looking really weird, and they will certainly take longer to
render on the visitors' screens. I've tried it, and it just doesn't work
well.)