Random Ad Display

Some advertising services automatically select different ads to place in your page each time a page on your site is displayed. That way they maximise the number of ads a given visitor sees and hence the possibility that they will click on one of those ads.

Where you have your own selection of ads to display then you also need your own way of selecting the ads to display so that a visitor to your site doesn't see the same ads displayed in the same place on each page. Now obviously we need to set aside a particular space on the page to display an ad but since ads generally come in standard sizes we can mix ads provided that we select ads that are all the same size.

Most frequently the ad code that a given advertiser will supply you with to place their ad in your page will consist either of an <a&><img></a> set of tags or will consist of a <script></script> that basically inserts the ad using javaScript (in each case there will be attributes within the tags indicating where to go when the <a> is clicked, what image to display or what script to run). Basically since you are usually given this block of code all we need worry about in setting things up to select randomly between these ads is the code needed to ensure that one or more of these blocks is inserted in the selected places in your page and that which is put where is selected randomly.

Having the ads selected at random is extremely easy to do if our web page supports PHP since all we need to do is to place the ads into an array, shuffle the array and then pull ads from the front of the shuffled array to display in our page.

Let's begin by creating some array entries for a few ads.

<?php
&ads[0] = '<a><img></a>';
$ads[1] = '<script></script>';
&ads[2] = '<a><img></a>';
$ads[3] = '<script></script>';

Just place each of the codes you are given for the ads into a separate line where the value is assigned to a $ads[] position starting with zero and going to one less than the total number of ads you have to choose from. Place this code right at the top of your web page (or if you want to use it in multiple pages then make it a separate file and include it at the top of each page.

We conclude that PHP code with the following:

shuffle($ads);
?>

So now we have all of our ads placed in an array when the page is loaded and they are shuffled into a random order so that at this point we know how many ads we placed in the array and we know what the ads are, we just don't know what order the ads are in.

So now all we need to do is to insert some code in each spot where we want an ad to be displayed. The code for the first position to display an ad is <?php echo $ads[0]; ?> and for the second spot where we want an ad to display we use <?php echo $ads[1]; ?> and so on for as many ads as we want to display (obviously we need more ads in the array than we are displaying or we will run out of ads to display).

Now this works to display ads at random in our web page and also has one major benefit over just selecting an ad at random for each spot where we want to display an ad because using this method of shuffling the ads rather than selecting at random means that we can have two or more ads in the same page and know that they will all be different ads.

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate