/*
**
Banner Rotation
S Babu: 05/22/01
v 1.0
Main include file for parsing the XML file and
displaying banners.
**
*/
$file = "banners.xml";
//Banner class. Essentially, load data from XML to this class.
Class Banner{
function Banner($nu,$picture,$description,$site) {
$this->nu = $nu;
$this->picture = $picture;
$this->description = $description;
$this->site = $site;
}
function show() {
//display a banner
print "site\">
picture\" height=\"175\" width=\"90\" border=\"0\" alt=\"" . addslashes($this->description) . "\">\n";
}
function jsshow() {
//print a javascript code to show the banner. SITEROOT is a JS variable
//that has the root URL of the site. Define it before this page is called.
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modifieed: ". gmdate("D, d M Y H:i:s"). " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Cache-Control: post-check=0,pre-check=0");
header("Cache-Control: max-age=0");
header("Pragma: no-cache");
print "document.write('
site\" class=\"banner\">
picture\" height=\"175\" width=\"90\" border=\"0\" alt=\"". addslashes($this->description). "\">
[More Information]
');\n";
}
}
function showall($records){
//Given an array of Banners show all the banners.
while($b = next($records)) $b->show();
}
function random($records){
//Given an array of Banners, pick a random one.
srand ((double) microtime() * 10000000); // set the seed for random
$choice = array_rand($records,1); // array_rand is a PHP4 function
$records[$choice]->jsshow();
}
// global variables.
$nu = 0;
$picture = '';
$description = '';
$site = '';
$currtag=''; // holds the name of the current tag being parsed
$banners = array(); // all Banner classes are pushed in here
//Function for XML parser, when a start tag is encountered.
function startElement($parser, $name, $attrs) {
global $nu, $picture, $description, $site, $currtag;
switch($name){
case "banner":
$nu = $attrs["nu"];
$currtag = '';
break;
default:
$currtag = $name;
break;
}
}
//XML processing, when a closing tag is encountered
//when a new is seen, push it to the array
function endElement($parser, $name) {
global $nu, $picture, $description, $site, $currtag;
global $banners;
$currtag = '';
switch($name){
case "banner":
$tmp = new Banner($nu,$picture,$description,$site);
array_push($banners,$tmp);
break;
default:
break;
}
}
//XML function for capturing data.
//Simply take the data and put it into variable named according
//to the current tag. This will produce undesirable results, if
//there are empty tags.
function characterData($parser, $data) {
global $nu, $picture, $description, $site, $currtag;
if ($currtag != '') {
${$currtag} = $data;
}
}
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
reset($banners);
?>