jump to content

The banner rotation system here has three different parts.

  1. banners.xml - this is a very simple XML file that defines the collection of banners.
  2. banner.inc - this is a PHP include file that parses the XML file and processes it.
  3. A PHP-Javascript file that picks up a random banner.

First, download the XML file from the link above. As you can see, it is a very simple one.

BANNERS
   BANNER nu="1"
     PICTURE bannerimage.gif
     SITE http://www.site.org
     DESCRIPTION This is a great open source project

One important point to note here is that the PHP code assumes that all these tags are well filled up.

Now, download the PHP include file from above. You can right-click and save the file.

If you look at the source, it is very simple. There is a class defined called Banner. It is the PHP equivalent of a single banner data in the XML file. Then I've defined as many global variables as there are different characteristics, for a banner. Though I hate global variables, that was an easy thing to do here. When tag data is found, I copy the data to a variable named like the tag name. When a tag is found in XML, I create a new instance of the Banner class, and push it into an array. Then there are functions that can show everything in the array (used in the front page of this section) and a random one from the array (used in the Javascript).

The Javascript is also quite simple. It is actually a PHP file that prints out Javascript code. Luckily, there is no rule that says Javascript source files must have a particular extension. That file goes like this.

<?
include("banner.inc");
random();
?>

All that is left to do is to call the Javascript file where you want a random banner to show up.

If all the pages in your site are PHP pages, you don't need Javascript at all. You can directly include the PHP file

I'll soon write how we can do the same thing in ASP with MSXML3.0 sp1 and XSLT. I would've preferred to do it in XSLT, but my site hosting provider does not Sablotronexternal link have XSL extensions compiled with PHP.