Display images randomly with javascript + PHP

Source: Internet
Author: User
Tags random seed

You will use javascript to randomly display images. But what if the image information (such as links) changes frequently or is modified or added by users? I have a solution here.
Put the image information (such as name, address, Link, etc.) in a TXT file (if MYSQL is better, or not ). The PHP program is called by javascript in HTML files, and the PHP program randomly reads image data. For the program, see:
Assume that the content of TP. TXT is as follows: (each field represents 'image name', 'link location', and 'image location' respectively, and TAB is used between fields)
OSO www.oso.com.cn www.oso.com.cn/image/logo.gif
163 www.163.com image.163.com/images/logo.gif
Sohu www.sohu.com www.sohu.com/image/logo.gif
Readrand. php (this program actually generates a javascript LANGUAGE)
<?
$ Arrayall = file ("tp.txt" when reading the content of tp.txt to the array
$ Arrays = count ($ arrayall );
If ($ arrays = 1) {// because rand (0, 0) is wrong
$ Selectrand = 0;
} Else {
Srand (double) microtime () * 1000000); // sets the Random Seed.
$ Selectrand = rand (0, $ arrays-1 );
}
$ Exstr = explode (chr (9), $ arrayall [$ selectrand]); // randomly retrieve and divide all
?>
Document. write ('<a href = "<? Echo $ exstr [1];?> "Target =" new "> "Width =" 200 "height =" 50 "alt =" <? Echo $ exstr [0];?> "> </A> ');
HTML file
<Html>
<Body>
<Script language = 'javascript 'src = 'readrand. php'>
</Script>
</Body>
</Html>
(You can place scripty to the location you need, and add the setTimeout () function to implement timed refresh)

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.