Home php教程 php手册 用PHP实现banner轮换的程序代码

用PHP实现banner轮换的程序代码

Jun 21, 2016 am 09:15 AM
image jpg link location of

程序

    我们曾经访问过这样的网站,每次刷新banner都随机的变化,在这篇文章中,我们将给大家介绍用PHP来实现这个功能。

  步骤

  程序实现的原理是:调用一个数组,每个图象对应一个数组中的元素,然后我们设置随机数,只要随机得到一个数据就可以显示一副图象了。

  第一个步是我们来产生一个随机数。每次刷新时我们都得到不同的随机数,具体代码为:

  srand((float) microtime() * 10000000);

  之后我们设置一个数组为image,然后再设置5个数组元素,代码如下:

  $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 '用PHP实现banner轮换的程序代码';

  把上面的代码组合起来就可以了。

  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 '用PHP实现banner轮换的程序代码';

  以上的代码是我们随机显示图片的代码,如果我们想使每个图片再加上各自的连接地址那么我们把上述的代码稍微改动下就可以了!把上述的数组改为二维数组:

  $image[1]['pic']='/location/of/image1.jpg';

  $image[1]['link']='/location/of/link1.php';

  相应的显示代码为:

  echo '';

  echo '用PHP实现banner轮换的程序代码';

  那么我们就可以完成我们标题的功能了,随机显示图片并且连接到不同的指定的地址:

  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 '用PHP实现banner轮换的程序代码';

  你可以把上面的代码拷到你的网页中去运行了。祝你好运



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to convert PNG to JPG on Windows 11 How to convert PNG to JPG on Windows 11 May 12, 2023 pm 03:55 PM

How to Convert PNG to JPG on Windows 11 On Windows 10 and 11, you can use Microsoft's built-in Paint app to quickly convert image files. To convert a PNG image to JPG on Windows 11, use the following steps: Open File Explorer and navigate to the PNG image you want to convert. Right-click the image and select Open With > Draw from the menu. Your photo or image opens in the Paint app. Note the file size at the bottom of the screen. To convert a file from PNG to JPG, click File and select Save As > JPEG Image from the menu. When the file resource

Fix: Can't open JPG files in Windows 11 Fix: Can't open JPG files in Windows 11 Apr 17, 2023 pm 12:37 PM

JPG is one of the most popular image file formats along with PNG. This is a lossy compressed image format ideal for websites. Therefore, many users save or convert images to JPG format. However, some users have stated that they are having issues opening JPG files in Windows 11 using the platform’s default Photos app. Therefore, these users cannot open, view and edit JPG format images in Windows 11. This is what one user said in a Microsoft forum post: When double-clicking a JPG file to open in Microsoft Photo, I receive the error The specified program cannot be found. This was purchased new about a month ago

Let's talk about how to use php to easily convert WebP to JPG. Let's talk about how to use php to easily convert WebP to JPG. Mar 24, 2023 pm 02:57 PM

In recent years, with the continuous development of Web technology, WebP, a new image format, has gradually entered the field of vision of programmers and Web developers. The advantages of WebP cannot be ignored: small file size, high quality, high browser support, etc. But in some cases, we may need to convert WebP images into some other formats, such as JPG. So, how to use PHP to convert WebP to JPG?

Analysis of location configuration examples in Nginx server Analysis of location configuration examples in Nginx server May 24, 2023 pm 02:05 PM

First, let me briefly introduce the types of location and matching rules, using the example of nginxwiki as an example: location=/{#matchesthequery/only.[configurationa]}location/{#matchesanyquery,sinceallqueriesbeginwith/,butregular#expressionsandanylongerconventionalblockswillbe#matchedfirst.[ configurationb]}location^~/im

What format is jpg? What format is jpg? Jun 21, 2023 am 11:36 AM

JPG is a JPEG format file. The JPEG format is the most commonly used image file format, with the suffix ".jpg" or ".jpeg". JPG format standards are divided into three types: JPEG, progressive JPEG, and JPEG2000. It is a standard generally used for continuous-tone static image compression. It is lossy compression, which compresses data images in a small storage space while causing loss of image data. Damage, if the user has higher requirements for image quality, it is recommended to abandon this image file format, especially for some high-definition pictures.

What does jpg mean? What does jpg mean? Nov 28, 2022 am 09:46 AM

JPG generally refers to the JPEG format, which is an image format saved using a lossy compression method. It is a standard for continuous tone static image compression. Its file suffix is ​​".jpg" or ".jpeg". JPG files mainly use a joint coding method of predictive coding (DPCM), discrete cosine transform (DCT) and entropy coding to remove redundant images and color data; it can compress images in a small storage space, but to a certain extent will cause damage to the image data.

How to use Bing Image Creator for free How to use Bing Image Creator for free Feb 27, 2024 am 11:04 AM

This article will introduce seven ways to get high-quality output using the free BingImageCreator. BingImageCreator (now known as ImageCreator for Microsoft Designer) is one of the great online artificial intelligence art generators. It generates highly realistic visual effects based on user prompts. The more specific, clear, and creative your prompts are, the better the results will be. BingImageCreator has made significant progress in creating high-quality images. It now uses Dall-E3 training mode, showing a higher level of detail and realism. However, its ability to consistently produce HD results depends on several factors, including fast

How to delete images from Xiaomi phones How to delete images from Xiaomi phones Mar 02, 2024 pm 05:34 PM

How to delete images on Xiaomi mobile phones? You can delete images on Xiaomi mobile phones, but most users don’t know how to delete images. Next is the tutorial on how to delete images on Xiaomi mobile phones brought by the editor. Interested users can come and join us. Let's see! How to delete images on Xiaomi mobile phone 1. First open the [Album] function in Xiaomi mobile phone; 2. Then check the unnecessary pictures and click the [Delete] button in the lower right corner; 3. Then click [Album] at the top to enter the special area , select [Recycle Bin]; 4. Then directly click [Empty Recycle Bin] as shown in the figure below; 5. Finally, directly click [Permanent Delete] to complete.

See all articles