The simplest way to display random banners/picture on your site is to use the rand () function and assign each image and link a number. The banner that corresponds to the random number generated is then displayed.
In this code we can assign an address for each image and a URL that it will link to. In our demonstration we have three banners, but you can use as many as you like, just follow this format.
Quote:
$Img1 = "http://yoursite.com/image.jpg";
$Url1 = "http://www.identity.st";
$Img2 = "http://www.hissite.com/bannerA.gif";
$Url2 = "http://www.about.com";
$Img3 = "http://hersite.com/MyImage.gif";
$Url3 = "http://php.about.com";
|
Change the url with ur needed page.
Next we will generate a random number and then build variables based on it.
We set rand to (1,3) however, if you choose to have more or less banners you need to change 3 to the exact amount you have.
Quote:
$num = rand (1,3);
$Image = ${'Img'.$num};
$URL = ${'Url'.$num};
|
Finally we print out the banner and link it to the appropriate URL
Quote:
Print " "; |
Here is the full code
Quote:
//By Angela Bradley for PHP @ About.com
//Assign the image and link URLs here, you can add more following this formatt
$Img1 = "http://goat.ws/board/images/avatars/114693804644304f35a8399.jpg";
$Url1 = "http://www.goat.ws";
$Img2 = "http://goat.ws/board/images/smiles/gamesc.gif";
$Url2 = "http://www.identity.st";
$Img3 = "http://goat.ws/board/images/avatars/11415438874159139eaacbe.gif";
$Url3 = "http://php.about.com";
//Leave the first number as 1, and set the second to how many banners you have
$num = rand (1,3);
//This creates the variables
$Image = ${'Img'.$num};
$URL = ${'Url'.$num};
//This actully outputs the banner
Print " ";
?>
|