Random Banner Script using PHP

I wanted to start alternating the banner advertisements on this site so started looking around for the simplest way to randomly display a different banner in my case a few lines of code that included the link, image and some text. I found a number of scripts that all seemed a bit clunky. I started thinking there must be a better simpler way.

All the solutions I found used the php function rand. We end up having to generate a random number from a range e.g. rand (0,100); will give us a random number between 1 and 100. There are a few problems here, firstly the range needs to be set to reflect the number of banners we have in our array so therefore we end up having to count the items in the array or use a fixed number.

The problem with rand is it deals with integers not blocks of code. I started looking for an alternative and stumbled upon shuffle. Shuffle as you would expect takes our array and reorders it at random as you do with a deck of cards. If you shuffle a deck of cards hen take the top card you end up with a random card from the pack each time. The other good thing about shuffle is it shuffles the whole array without us having to specify the number of entries.

So here is the code:

<?php
$advert = array();
  $advert[] = '<a href="#">Banner 1</a>';
  $advert[] = '<a href="#">Banner 2</a>';
  $advert[] = '<a href="#">Banner 3</a>';
shuffle($advert);
echo $advert[0];
?>

Now lets have a look at what we have done line by line.

$advert = array(); Begin a new array named “advert”

$advert[] = '<a href="#">Banner 1</a>'; Create our first advert. Put the HTML code of the advert within an $advert[] variable. The great thing is we can add as many adverts as we want as it is an array and therefore will not overwrite the variable. The example shows three but you can have as many as you like.

shuffle($advert); The PHP function, shuffle, that will randomise our advertisements.

echo $advert[0]; We echo the first advert in the array. The array has been shuffled so the first advert will change.

To use this script your page will need to be able to parse PHP. Usually this means your page will have a .php extension so the server knows it contains code that should be executed before the page is delivered to the users browser.

You can see an example of this script working for the banner advert below. If you refresh this page you will see the banner randomly changes. The same one may appear twice in a row as there is as much chance of having the same banner at the top of the pile as there is for the other one to appear.

This article was posted on 08/27 in Code, PHP, Tutorials

Try a sample pack of 10 Business Cards for free from MOO

Upload images for the first side of your card. Choose up to 10 different images or designs. We accept high resolution JPEG, PNG, GIF or PDF files. Free standard shipping is included.

comments

add comment

What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What you have had to say about all this... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What you have had to say about all this... What you have had to say about all this... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What you have had to say about all this... What you have had to say about all this... What you have had to say about all this... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What you have had to say about all this... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What you have had to say about all this... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What you have had to say about all this... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What you have had to say about all this... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What you have had to say about all this... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What's the matter cat got your tongue? Be the first to comment... What you have had to say about all this... What you have had to say about all this... What's the matter cat got your tongue? Be the first to comment... What you have had to say about all this... What you have had to say about all this...

That's the end of this article. I hope you found it useful. If you're enjoyed this article why don't you have a look around the archives, where you can find some more tutorials, tips and general ramblings.