php1I have posted a new and much better script for rotating banners. jQuery Featured Post Slider

There are countless ways to create a banner ad rotator script in several different coding languages. For making an ad rotator I’d prefer to do it with PHP like I have done below, but depending on where I wanted to display it, I might consider using jQuery because that is becoming one of my new favorite things and is growing more popular everyday. This is just a basic PHP ad rotating script with nothing fancy like what you could do with jQuery, but it is still useful and rather simple to write.

Open up a new text document in notepad or Dreamweaver, whatever it is you would use, and create a new file named adrotator.php. On the top 2 lines type this:

1
2
3
<?php

$bannercounter= 1;

Then, you need to create a variable for your banner ad’s HTML code, and increase the banner counter variable depending on the number of banners you want to display or in otherwords number of $bannercode variables you create. You will see what I mean in a minute. Increase the counter via the bannercode variable like this:

1
2
$bannercode[$bannercounter] = "put the ad HTML code here";
$bannerounter++;

So say you wanted to rotate 3 banner ads, you would rewrite this 3 times with each banners HTML code for the $bannercode variable. Be sure to make sure you get the quotation marks correct too.
I like to use singles ‘ ‘ for variables like $variable = ‘this’. That way any HTML you may have between them you can just use double guotes and not have to backslash escape all of them like .

This is how you would write the code for each banner.

1
2
3
4
5
6
7
8
9
10
11
// Banner One
$bannercode[$bannercounter] = '<a href="http://tweeaks.com/category/development/"><img src="http://tweeaks.com/wp-content/uploads/2009/10/fbconnect.png" alt="Banner One" /></a>';
$bannercounter++;

// Banner Two
$bannercode[$bannercounter] = '<a href="http://tweeaks.com/category/development/"><img src="http://tweeaks.com/wp-content/uploads/2009/10/get-rss.png" alt="Banner Two" /></a>';
$bannercounter++;

// Banner Three
$bannercode[$bannercounter] = '<a href="http://tweeaks.com/portfolio"><img src="http://tweeaks.com/wp-content/uploads/2009/10/get-email.png" alt="Banner Three" /></a>';
$bannercounter++;

Now we need to get the total number of ads, and randomly display them if there are more than one, otherwise just show one which can be good to specify just in case you remove other ads and only one is left.

1
2
3
4
5
6
7
8
9
10
11
12
$toatalads = $bannercounter - 1;

if ($totalads &gt; 1) { //if total number of ads is greater than 1 then randomly show them

mt_srand ((double) microtime() * 1234567 );
$pickedbanner = mt_rand(1, $totalads);

} else { // otherwise show just one ad

$pickedbanner = 1;
}
$bannerad = $bannercode($pickedbanner);

That’s it for the rotator script. Save your adrotator.php file and upload it. Then where ever you want to have the banners show up just do an include of that file like this:

1
2
3
4
5
include ('adrotator.php');
echo "
<div>$bannerad</div>
"
;
?>

That’s all there is to it. If you have any problems getting this to work let me know and I can help you out. It’s not the most advanced script, but it does the trick quick and easily. I plan to do a follow up post to this with a Javascript or maybe jQuery tutorial if I’m in the mood.

So once again here is the entire script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

$bannercounter= 1;

// Banner One
$bannercode[$bannercounter] = '<a href="http://tweeaks.com/category/development/"><img src="http://tweeaks.com/wp-content/uploads/2009/10/fbconnect.png" alt="Banner One" /></a>';
$bannercounter++;

// Banner Two
$bannercode[$bannercounter] = '<a href="http://tweeaks.com/category/development/"><img src="http://tweeaks.com/wp-content/uploads/2009/10/get-rss.png" alt="Banner Two" /></a>';
$bannercounter++;

// Banner Three
$bannercode[$bannercounter] = '<a href="http://tweeaks.com/portfolio"><img src="http://tweeaks.com/wp-content/uploads/2009/10/get-email.png" alt="Banner Three" /></a>';
$bannercounter++;

$toatalads = $bannercounter - 1;

if ($totalads &gt; 1) { //if total number of ads is greater than 1 then randomly show them

mt_srand ((double) microtime() * 1234567 );
$pickedbanner = mt_rand(1, $totalads);

} else { // otherwise show just one ad

$pickedbanner = 1;
} // end if
$bannerad = $bannercode($pickedbanner);

?>

Then this is placed where you want it to show up, making sure to point to the adrotator.php file on your server.
[cc lang="php"]
include ('adrotator.php'); // adrotator.php script on your server
echo "

$bannerad

“;
?>

I will create a demo page shortly for this and the JavaScript banner rotator script.

Get automatic updates! Subscribe to Our RSS Feed or Get Email Updates sent straight to your inbox!