Location: PHPKode > scripts > Banner Ad Rotation Script > Ads.php
<?PHP
// Functions:

// Gives today's date with no time
function todaysDate()
{
	list($dd,$mm,$yyyy) = split("-",date("d-m-Y",time()));
	return makeDateValue($dd,$mm,$yyyy);
}

function makeDateValue($dd,$mm,$yyyy)
{
	return mktime(0, 0, 0, $mm, $dd, $yyyy);
}


?>
<SCRIPT LANGUAGE="JavaScript">
// Setup
var curAd;
var ads = new Array();
var delays = new Array();
var linx = new Array();
var external = new Array();
function displayAd()
{
	var adLink = document.getElementById("ADLINK");
	var ad = document.getElementById("AD");
	if (ads.length)
	{	
		ad.src = "./Ads/"+ads[curAd];
		adLink.href=linx[curAd];
		if (external[curAd])
		{
			adLink.target="_blank";
		}
		else
		{
			// It's one of our own!
			adLink.target="_self";
		}
		if (ads.length > 1)
			// Skip refresh if we only have 1 ad!
			setTimeout("displayAd()",delays[curAd]*1000);
		// Move on
		curAd = (++curAd) % ads.length;
	}
}
</SCRIPT>

<DIV align=center>
<A id=ADLINK href="#">
<IMG id=AD border=0 src="#">
</A>

<SCRIPT LANGUAGE="JavaScript">
// Setup
var i = 0;

// PHP generated JavaScript array:
<?php
$fd = fopen ("./Ads/ads.csv", "r");
if ($fd)
{
	$adsUsed = 0;
	$today = todaysDate();
	$line = null;
	while (!feof ($fd)) 
	{
		if (!$line)
		{
			// Skip header
			$line = fgets($fd, 4096);
		}
		else
		{
			$line = fgets($fd, 4096);
			if (strlen($line) > 10)
			{
				list($startDate,$durationMonths,$adDurationSecs,$pipeSeparatedImages,$URL) = split (",", $line, 5);
				list($dd,$mm,$yyyy)= split ("\/", $startDate, 3); 
				$startDateTime = mktime(0, 0, 0, $mm, $dd, $yyyy);
				$endDateTime = mktime(0, 0, 0, $mm + $durationMonths, $dd, $yyyy);
				if ($durationMonths < 0 || ($durationMonths > 0 && ($startDateTime <= $today ) && ($today <= $endDateTime)))
				{
					// We've got a contender
					++$adsUsed;
					$image = split("\|",$pipeSeparatedImages);
					foreach($image as $anImage)
					{
						if (strtolower(substr($URL,0,7))=="http://")
						{
							echo "external[i]=true;\n";
						}
						echo "ads[i] = \"$anImage\";\n";
						echo "linx[i] = \"".rtrim($URL)."\";\n";
						echo "delays[i++] = $adDurationSecs;\n";
					}
				}
			}
		}
	}
	fclose($fd);
}
?>
// Prevent same start ad being shown on every new page.. add random element
curAd = <?php echo rand(0,$adsUsed - 1); ?>;
displayAd();
</SCRIPT>
</DIV>
Return current item: Banner Ad Rotation Script