Print

Printed May 24, 2013 from B&T's Tips & Scripts
http://tips-scripts.com


Random/weighted banners (images), quotes & more

This script will display banners (with or without links), quotes, or anything else you want to randomly select with each individual page load.   The item will be randomly selected, but the frequency of selection can be weighted based on a value you specify.   For example, giving an item a weight of 2 will cause that item to be selected twice as often as an item with a weight of 1.   Use only integers 1 or greater for weight values.

You can specify the item as html code in the script, or have each item be stored in an external file (also as html code).   The switch at the top of the script makes the selection of in-script or external files.   The example below shows the in-script method.

You can download this script as a .txt file.  Remember to rename the file as a .php file.

- - Start Script Here - -
<?php
$externalFile 
"N";  // Y means the item array contains file names
// add to the weight and item arrays below
$weight[0] = 1$item[0] = "<a href='link0.htm'><img src='image0.gif' alt='' /></a>";
$weight[1] = 1$item[1] = "<a href='link1.htm'><img src='image1.gif' alt='' /></a>";
$weight[2] = 1$item[2] = "<a href='link2.htm'><img src='image2.gif' alt='' /></a>";
// end of arrays
for ( $a=0$a<count($weight); $a++ ) 
for ( 
$b=1$b<=$weight[$a]; $b++ ) $pick[] = $a;
$selected $item[$pick[rand(0,count($pick)-1)]];
if (
$externalFile == "Y"$selected file_get_contents($selected);
echo 
$selected;
?>

- - End Script Here - -

If you wish each item to be stored in an external file, then you set $externalFile = "Y"; and change the array values to file names as in the example below:
$weight[0] = 1; $item[0] = "file0.txt";