想必你會用javascript隨機顯示圖片。但如果圖片的資料(比如鏈接)是經(jīng)常變化,或是由用戶修改、增加的,怎么辦?我這里有個辦法。 把圖片的資料(如名稱、地址、鏈接等)放在一個TXT文件里(如有MYSQL更好,沒有也罷)。HTML文件中由javascript調(diào)用PHP程序,PHP程序隨機讀取圖片資料。程序見下: 假設(shè)有TP.TXT內(nèi)容如下:(各字段分別代表‘圖片名稱’、‘鏈接位置’、‘圖片位置’,字段間為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(此程序?qū)嶋H上是生成一句javascript語言) <? $arrayall=file("tp.txt");讀出tp.txt內(nèi)容到數(shù)組 $arrays=count($arrayall); if ($arrays==1){//because rand(0,0) is wrong $selectrand=0; }else{ srand((double)microtime()*1000000);//設(shè)定隨機數(shù)種子 $selectrand=rand(0,$arrays-1); } $exstr=explode(chr(9),$arrayall[$selectrand]);//從全部中隨機取出一個并分割 ?> document.write('<a href="<? echo $exstr[1];?>" target="new"><img src="<? echo $exstr[2];?>" width="200" height="50" alt="<? echo $exstr[0];?>" ></a>');
HTML文件 <html> <body> <script language='javascript' src='readrand.php'> </script> </body> </html> (你可以把scripty放到你需要的位置,并可以加入setTimeout()函數(shù)以實現(xiàn)定時刷新)
|