Home  >  Article  >  Backend Development  >  Use javascript+PHP to randomly display images_PHP tutorial

Use javascript+PHP to randomly display images_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:04:55910browse

Presumably you will use javascript to display images randomly. But what if the image information (such as a link) changes frequently, or is modified or added by the user? I have a solution here.
Put the image information (such as name, address, link, etc.) in a TXT file (MYSQL is better if it is available, but it doesn’t matter if it is not available). The PHP program is called by javascript in the HTML file, and the PHP program randomly reads the image data. See the program below:
Assume that there is TP.TXT with the following content: (each field represents 'picture name', 'link position', 'picture position', and there are TABs 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 sentence of javascript language)
$arrayall=file("tp.txt"); Read the content of tp.txt into the array
$arrays=count($arrayall);
if ($arrays==1){//because rand(0,0) is wrong
$selectrand=0;
}else{
srand((double)microtime()*1000000);//Set random number seed
$selectrand=rand(0,$arrays-1);
}
$exstr=explode(chr (9),$arrayall[$selectrand]);//Randomly pick one out of all and split it
?>
document.write('<? echo $exstr[0];?>');
HTML file





(You can put scripty where you need it, and Add the setTimeout() function to achieve regular refresh)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445151.htmlTechArticlePresumably you will use javascript to randomly display images. But what if the image information (such as a link) changes frequently, or is modified or added by the user? I have a solution here. Put the picture...
Statement:
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