April 19th, 2007
Poor man’s banner / ad rotator
So you want to add advertisement on your site, but do not want to show the same old advertisement banner all the time? Well heres an easy and simple solution in PHP that even a someone who copies and pastes the code from a blog site can do… hey.. wait!
Say you have one image banner somewhere on your site.
Cool banner:
<a href=”http://www.bluechiphosting.com” mce_href=”http://www.bluechiphosting.com”>
<img src=”banner.jpg” border=”0″ alt=”my cool banner”>
</a>
other cool banner:
<a href=”http://www.bluechiphosting.com” mce_href=”http://www.bluechiphosting.com”>
<img src=”otherbanner.jpg” border=”0″ alt=”my other cool banner”>
</a>
Place the 2 banner codes from above as shown below in your PHP site:
<?php
$rand = rand(0, 1);
if ($rand == 0) {
echo ‘<a href=http://www.bluechiphosting.com><img src=banner.jpg border=0 alt=my cool banner></a>’;
} else if ($rand == 1) {
echo ‘<a href=http://www.bluechiphosting.com><img src=otherbanner.jpg border=0 alt=my other cool banner></a>’;
}
?>
And that’s it!
You could grow this into potentially big script that has many images, you could also just make text links.. or an entire block of html code.
This is basicly using the rand() funtion in PHP to pick a random number between 0 and 1, if 0 the 1st image is shown, if its 1 the 2nd image is shown.
larger sample:
<?php
$rand = rand(0, 6);
if ($rand == 0) {
echo ‘ONE’;
} else if ($rand == 1) {
echo ‘TWO’;
} else if ($rand == 2) {
echo ‘THREE’;
} else if ($rand == 3) {
echo ‘FOUR’;
} else if ($rand == 4) {
echo ‘FIVE’;
} else if ($rand == 5) {
echo ‘SIX’;
} else if ($rand == 6) {
echo ‘and SO ON’;
}
?>
That example shows using rand() chooing a number from 0 to 6, hope it helps to understand it.
Hope you enjoy our poor man’s banner/ad rotation script! We never said it was pretty! we said it was easy!
Author’s notes: Watch the single quotes! Might want to make sure they are still quotes if you copy and paste! Have fun!
Poor man’s 3d button with pure CSS