Program we have visited such a site, each refresh banner random changes, in this article, we will introduce you to use PHP to achieve this function.
Steps
The principle of program implementation is: Call an array, each image corresponds to an array of elements, and then we set the random number, as long as a random to get a data can display an image.
The first step is for us to produce a random number. Each time we refresh, we get a different random number, the specific code is:
Srand ((float) microtime () * 10000000);
Then we set an array to image and then set 5 elements of the array, the code is as follows:
$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 ';
The following code implements the function of randomly selecting an element from an array:
$rn = Array_rand ($image);
And then we'll show you a random picture:
Echo ' ';
Put the above code 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 ' ';
The above code is our random display of pictures of the code, if we want to make each picture plus their respective connection address so we have to change the above code slightly! Change the above array to a two-dimensional array:
$image[1][' pic ']= '/location/of/image1.jpg ';
$image[1][' link ']= '/location/of/link1.php ';
The corresponding display code is:
Echo ' <a href= '. $image[$rn][' link '. ' > ';
Echo ' ';
Then we can complete the function of our title, randomly display the picture and connect to a different specified address:
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 ' ';
You can copy the code above to your Web page to run it. Good luck