Heim  >  Artikel  >  Backend-Entwicklung  >  用javascript+PHP随机显示图片_PHP教程

用javascript+PHP随机显示图片_PHP教程

WBOY
WBOYOriginal
2016-07-20 11:04:55907Durchsuche

想必你会用javascript随机显示图片。但如果图片的资料(比如链接)是经常变化,或是由用户修改、增加的,怎么办?我这里有个办法。
把图片的资料(如名称、地址、链接等)放在一个TXT文件里(如有MYSQL更好,没有也罢)。HTML文件中由javascript调用PHP程序,PHP程序随机读取图片资料。程序见下:
假设有TP.TXT内容如下:(各字段分别代表‘图片名称’、‘链接位置’、‘图片位置’,字段间为TAB)
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(此程序实际上是生成一句javascript语言)

$arrayall=file("tp.txt");读出tp.txt内容到数组
$arrays=count($arrayall);
if ($arrays==1){//because rand(0,0) is wrong
$selectrand=0;
}else{
srand((double)microtime()*1000000);//设定随机数种子
$selectrand=rand(0,$arrays-1);
}
$exstr=explode(chr(9),$arrayall[$selectrand]);//从全部中随机取出一个并分割
?>
document.write('<? echo $exstr[0];?>');
HTML文件





(你可以把scripty放到你需要的位置,并可以加入setTimeout()函数以实现定时刷新)

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445151.htmlTechArticle想必你会用javascript随机显示图片。但如果图片的资料(比如链接)是经常变化,或是由用户修改、增加的,怎么办?我这里有个办法。 把图片...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn