View Full Version : Random Script
freefunonline
10-12-2006, 03:25 AM
I was wondering if anyone knows where I would be able to download a random php script.
Everytme some refreshes the page a new image shows.But I want so that you can click on the image and it brings you to a certain page. Many humor/media sites have them.
Any help would be great.
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.
$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.
$num = rand (1,3);
$Image = ${'Img'.$num};
$URL = ${'Url'.$num};
Finally we print out the banner and link it to the appropriate URL
Print "<a href=".$URL."><img src=".$Image." border=0></a>";
Here is the full code
<?php
//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 "<a href=".$URL."><img src=".$Image." border=0></a>";
?>
freefunonline
10-12-2006, 08:14 PM
Thanks a lot for the help Guru.:)
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.