I'm in the coding mood today 🙂 .
I cannot count how many times I've been asked to code a UBER simple banner rotator script.
These things are so simple, but if you don't know PHP it can be difficult to understand.
The script below has two variables:
$links = array(
"http://www.google.com",
"http://www.msn.com",
"http://forums.digitalpoint.com"
);
//add a new link for the banner to a new line in the same format as above
and:
$images = array(
"http://www.google.co.uk/intl/en_uk/images/logo.gif",
"http://stc.msn.com/br/hp/en-us/css/35/decoration/msn_b.gif",
"http://forums.digitalpoint.com/images/misc/dps_logo.gif"
);
//add a new image link for the banner to a new line in the same format as above
As you can see they match up. The first line for the $links variable is for a google image, the second is for a msn image etc.
Here is the full code:
<?php
session_start();
$links = array(
"http://www.google.com",
"http://www.msn.com",
"http://forums.digitalpoint.com"
);
//add a new link for the banner to a new line in the same format as above
$images = array(
"http://www.google.co.uk/intl/en_uk/images/logo.gif",
"http://stc.msn.com/br/hp/en-us/css/35/decoration/msn_b.gif",
"http://forums.digitalpoint.com/images/misc/dps_logo.gif"
);
//add a new image link for the banner to a new line in the same format as above
//-------- DO NOT EDIT BELOW THIS LINE----------
$count = count($links) -1;
$randnum = mt_rand(0,$count);
if ($randnum == $_SESSION['randnum'])
{
$randnum = mt_rand(0,$count);
}
$_SESSION['randnum'] = $randnum;
echo '<a href="'.$links[$randnum].'"><img style="border:none" src="'.$images[$randnum].'" /></a>';
?>
The code has been constructed so that the likelihood of getting the same banner displayed twice is greatly reduced.
Comment here if you like the script, or have any questions 😀
You can see an example of the script running here.
Hi,
Thanks for this script. It is very simple, but might just fir the bill for me.
I had to replace the apostrophes (single quotes) in the echo statement for it to work. Those slanteed ones created a parse error.
echo (‘‘);
also had to take the spaces out of the style attribute. My just be my hosting plan.
See it here: http://donhmorris.com/sbr.php
(Same links as script; no change.)
Okay, thanks.
Warmly,
Don 🙂
Thank you for highlighting that problem. It appears to be a wordpress issue. I will try to sort it out 🙂
Thank you for coming to my blog 😀
Do you have an example of this running??
I have updated the post to contain a link to the script 🙂
Here.
Just an update. I have fixed the quote problem, so people can copy and paste code from this site without problems now 😀
Thanks for this wonderful script, I will use it
Minimalistic and superb! Copypasted and worked just out-of-the-box. Thanks for sharing.
I’m glad you like it 🙂
Thank you for commenting.
Thanyou so much, i’d like if you have something exactly like this that can be integrated into a wordpress plugin. Again, thanks for this piece of “gold code”
is it possible to create a javascript version of this script ? i have a gamesitescript which is using cache function, so php don’t work. thanks
Hi,great code.works perfect.
What can i add to the code so,
when the banner is clicked , the browser
will open up in a new window. Prefer to
have my site and the link open at the same
time. thank u kindly
Hi,
Simply add target=”_blank” to your href code.
Pingback: My Homepage