How to: create an ads rotator with php

Some days ago, I was looking for an original way to display my favorites premium WordPress themes in the footer of my other blog WpRecipes. I finally created the following script.


The problem

As I said, I wanted to be able to display a group of 4 premium WordPress themes in the footer of WpRecipes. Nothing hard with this. But, I also wanted to be able to add as many themes as I’d like. Nevermind how many themes I added, only 4 must be displayed at the same time.
And of course, I wanted this script to be easy to maintain: I don’t have time to loose each time I’ll want to add a new theme.

The solution

After thinking a bit, I came up with the solution of a rotator which will take X items, sort them randomly and pick up 4 of them for being displayed.
To achieve this, I will create a php array which will contains another array for each item, with infos as such as title, image and url.
Each item will be displayed with a html unordered list, which will allow us to use css to control the display and give our item the look and feel we want.

Create an ad rotator with php

The code

Here’s the code used. You just have to paste it where you want the ad rotator to appear. Or even better, you can create a function with it.

<?php
$themes = array( 
	array( title => "ambience",
	       img => "wp-content/uploads/themes/ambience.jpg", 
               url => "http://www.wprecipes.com/freshnews.html",
        ),
        array( title => "real estate",
	       img => "wp-content/uploads/themes/realestate.jpg", 
               url => "http://www.wprecipes.com/realestate-theme.html",
        ),
	array( title => "wpvybe",
	       img => "wp-content/uploads/themes/wpvybe.jpg", 
               url => "http://www.wprecipes.com/wpvybe.html",
        ),
	array( title => "freshnews",
	       img => "wp-content/uploads/themes/freshnews.jpg", 
               url => "http://www.wprecipes.com/freshnews.html",
        ),
);
//It will me much better with a foreach loop here, someone submits the code?
$rand_keys = array_rand($themes, 4);
echo '<a href="'.$themes[$rand_keys[0]][url].'" target="blank"><img src="'.$themes[$rand_keys[0]][img].'" alt="" /></a>';
echo '<a href="'.$themes[$rand_keys[1]][url].'" target="blank"><img src="'.$themes[$rand_keys[1]][img].'" alt="" /></a>';
echo '<a href="'.$themes[$rand_keys[2]][url].'" target="blank"><img src="'.$themes[$rand_keys[2]][img].'" alt="" /></a>';
echo '<a href="'.$themes[$rand_keys[3]][url].'" target="blank"><img src="'.$themes[$rand_keys[3]][img].'" alt="" /></a>';

?>

Adding a new item

Adding a new item to the rotator is very easy. You just have to add a new ellement to the $themes array:

array( title => "Item title",
	  img => "image url", 
          url => "url",
        ),

Usage and demo

A “live demo” of this code can be found on the footer of my other blog WpRecipes.
I chose to use this code to display an inline selection of premium WordPress themes, but you can use it for many others usages, as such as displaying 125*125px ads, or create a rotating blogroll for your WordPress blog.