Web Site Development Resources Directory
 
Advanced Search

Random banner rotation in PHP

Author: Steve
WebSite: http://www.websitegravy.com

Have you ever visited a web site where, each time you refresh the page, the banner changes? In this article, we will take a look at how someone like you can implement this feature using PHP. The beauty of PHP is its ease of use. Depending on the number of images you wish to rotate, this script is quite compact and fast acting.

The Steps

This script will need to automatically display a single image out of a collection of available images. The easiest way to tackle this is to use a single array. This way, we can generate a random number, then call the particular image, which we saved within the array data structure.

The first step is seeding the random number generate. Seeding will instruct the random number generator to find a random number based, in this case, on microseconds. This ensures we will receive a truly random number each time the page is loaded, although it is still possible to generate the same random number more than once, consecutively.

To seed the random number generation, we use the srand() function. Consider the following code:

srand((float) microtime() * 10000000);

We are taking in a float as the seed in this case, which is generated by microtime() * 10000000. The microtime() function returns the current UNIX timestamp, in microseconds.

Next, let's declare our array. I will name my array image, for clarity, and will declare 5 different images.

$image[1]='/location/of/image1.jpg';
$image[2]='/location/of/image2.jpg';
$image[3]='/location/of/image3.jpg';
$image[4]='/location/of/image4.jpg';
$image[5]='/location/of/image5.jpg';


So, our array now contains 5 different array elements, holding the location of 5 different images. Now, are job is to generate a random number, in this case between 1 and 5, inclusive.

$rn = array_rand($image);

The array_rand() will do just what we want, generate a random number based on the number of total occupied array positions. We store that number in the $rn variable, which stands for random number. Let's actually print the statement now that will display the random image.

echo '<img src="'.$image[$rn].'">';

Notice that we are placing our randomly generated number within our array call, which will then call that position within the array. Let's now put the entire script together.

srand((float) microtime() * 10000000);

  $image[1]='/location/of/image1.jpg';
  $image[2]='/location/of/image2.jpg';
  $image[3]='/location/of/image3.jpg';
  $image[4]='/location/of/image4.jpg';
  $image[5]='/location/of/image5.jpg';

  $rn = array_rand($image);

echo '<img src="'.$image[$rn].'">';


Okay, this is well and good to display an image without a link. But, what if you want provide a separate link for each image? How do we accomplish this? Create a new array? Well, we could, but instead we'll simply change our current array into a multidimensional array for our purposes. Here's how the modified array looks.

$image[1]['pic']='/location/of/image1.jpg';
$image[1]['link']='/location/of/link1.php';


This means that within our first array position, there are two different directions we can take, pic and link. You can think of a multidimensional array as storing two separate bits of information in a single array element, although understand these elements are indeed stored in separate elements. Let's change our echo statement, now, to reflect the change.

echo '<a href="'.$image[$rn]['link'].'">';
echo '<img src="'.$image[$rn]['pic'].'">';


Again, let's put this together.

srand((float) microtime() * 10000000);

  $image[1]['pic']='/location/of/image1.jpg';
  $image[1]['link']='/location/of/link1.php';

  $image[2]['pic']='/location/of/image2.jpg';
  $image[2]['link']='/location/of/link2.php';

  $image[3]['pic']='/location/of/image3.jpg';
  $image[3]['link']='/location/of/link3.php';

  $image[4]['pic']='/location/of/image4.jpg';
  $image[4]['link']='/location/of/link4.php';

  $image[5]['pic']='/location/of/image5.jpg';
  $image[5]['link']='/location/of/link5.php';

  $rn = array_rand($image);

echo '<a href="'.$image[$rn]['link'].'">';
echo '<img src="'.$image[$rn]['pic'].'">';


And so, now you should be able to create your own random image script for your web site.

Have fun.

 

 


Best Catalog'
partners :
Best Catalog'
friends :
© 2004 BestCatalog.net