Use PHP to implement banner rotation program code

Source: Internet
Author: User
The original content of this site. for reprinted content, please indicate the source webpage. We once visited such a website and randomly changed every time we refreshed the banner. in this article, we will introduce you to using PHP to implement this function. The principle of the procedure is: call an array, each image corresponds to an element in an array, and then we set a random number, as long as the original content of the site is randomly obtained, reprinted please indicate the source Webpage Tutorial network.

We once visited such a website and randomly changed every time we refreshed the banner. in this article, we will introduce you to using PHP to implement this function.

Procedure

Program Implementation principle: call an array, each image corresponds to an element in an array, then we set a random number, as long as a random data can be displayed a pair of images.

The first step is to generate a random number. Each time we refresh, we get different random numbers. the specific code is:

Srand (float) microtime () * 10000000 );

Then we set an array to image, and then set five array elements. 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 randomly selects an element from the array:

$ Rn = array_rand ($ image );

Then we will display a random image:

Echo '';

Combine the above code.

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 the code for randomly displaying images. if we want to add a connection address for each image, we can modify the above code slightly! Change the preceding 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 '';

Echo '';

Then we can complete our title function, randomly display images and connect them to different specified addresses:

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 '';

Echo '';

You can copy the above code to your webpage 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.