Using PHP to achieve banner rotation of the program code

Source: Internet
Author: User
Tags array
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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.