PHP
  Home arrow PHP arrow Dynamic Generation of Menu Structures and JavaScript Rollovers in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Dynamic Generation of Menu Structures and JavaScript Rollovers in PHP
By: Chris Mospaw
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    1999-05-10


    Table of Contents:
  • Dynamic Generation of Menu Structures and JavaScript Rollovers in PHP
  • Making a Template
  • Starting to Automate
  • The power of repetition
  • Ordering up a Menu
  • Adding Pages

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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.)


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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT