Home >Backend Development >PHP Tutorial >Use javascript+PHP to randomly display images_PHP tutorial
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('');
HTML file