PHP 从现有代码中随机化文件列表数组
PHP randomise file list array from existing code?
正在浏览器窗口中创建全屏幻灯片放映
在javascriptkit上找到了这两段优秀的代码
http://www.javascriptkit.com/javatutors/externalphp2.shtml
php生成图像文件数组,javascript/css/html运行节目。
我已经编辑了css/html,以便图像填满窗口。这部分不是问题。
我希望 php 每次运行时都生成一个随机列表。似乎洗牌($files);是答案(?),但是无论我把它放在 php 文件中的哪个地方,它都不起作用。
<?
//PHP SCRIPT: getimages.php
Header("content-type: application/x-javascript");
//This function gets the file names of all images in the current directory
//and ouputs them as a JavaScript array
function returnimages($dirname=".") {
$pattern="('.jpg$)|('.png$)|('.jpeg$)|('.gif$)"; //valid image extensions
$files = array();
$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){ //if this file is a valid image
//Output it as a JavaScript array element
echo 'galleryarray['.$curimage.']="'.$file .'";';
$curimage++;
}
}
closedir($handle);
}
return($files);
}
echo 'var galleryarray=new Array();'; //Define array in JavaScript
returnimages() //Output the array elements containing the image file names
?>
有人可以帮我洗牌生成的文件列表数组吗?
谢谢
.html:
<script src="pics/getimages.php"></script>
<script type="text/javascript">
var curimg=0
function rotateimages(){
document.getElementById("slideshow").setAttribute("src", "pics/"+galleryarray[curimg])
curimg=(curimg<galleryarray.length-1)? curimg+1 : 0
}
window.onload=function(){
setInterval("rotateimages()", 5000)
}
</script>
<div>
<img id="slideshow" src="pics/animal_0001.jpg" />
</div>
原始 php 文件的输出:
var galleryarray=new Array();
galleryarray[0]="nature_0001.jpg";
galleryarray[1]="animal_0107.jpg";
galleryarray[2]="nature_0007.jpg";
galleryarray[3]="nature_0083.jpg";
galleryarray[4]="animal_0120.jpg";
galleryarray[5]="nature_0025.jpg";
galleryarray[6]="summertime.jpg";
galleryarray[7]="nature_0091.jpg";
galleryarray[8]="travel_0018.jpg";
galleryarray[9]="animal_0052.jpg";
galleryarray[10]="travel_0038.jpg";
galleryarray[11]="animal_0047.jpg";
galleryarray[12]="nature_0016.jpg";
galleryarray[13]="nature_0024.jpg";
galleryarray[14]="travel_0053.jpg";
修订版的输出:
var galleryarray = true;
治疗:
http://www.tsah.co.uk/tsah/slide/slides.html
你自己构建JS使事情过于复杂。尝试json_encode:
<?php
function returnimages($dirname=".") {
$images = array();
$pattern="('.jpg$)|('.png$)|('.jpeg$)|('.gif$)"; //valid image extensions
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){ //if this file is a valid image
$images[] = $file;
}
}
closedir($handle);
}
return $images;
}
$images = returnimages();
shuffle($images);
Header("content-type: application/x-javascript");
echo 'var galleryarray = ' . json_encode( $images ) . ';';
?>
shuffle 不会返回 shuffled 数组,而是返回操作的状态(TRUE/FALSE)。
相关文章:
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- 将键盘使用与 Qualtrics 中的答案随机化相结合
- 我怎样才能随机化 HTML 元素的颜色属性
- 通过AJAX调用PHP随机化器的输出
- 如何在JS中随机化backgroundPosition而不使用鼠标移动来制作万花筒的动画
- 使用Javascript对RGBA颜色进行光标控制的形状随机化
- 如何在一个随机化的有序列表中随机化多个无序列表
- 如何使用JavaScript随机化有序列表
- 使用JavaScript随机化html标记
- 可以'我的单词随机化器脚本不能正常循环
- 如果语句是假的 JavaScript,则再次随机化数字
- 函数随机化 - 我可以让它错过一个页面并且该页面上只有一个图像吗?可湿性粉剂
- 从包含文件夹的文件夹中随机选择文件,然后选择文件
- 随机化网页上的图像和颜色对
- 随机侮辱生成器(随机化结果)
- Javascript:通过在while循环内推送/拼接来随机化数组
- 构建一个简单的随机化器,其中包括一个点、一个下划线、字母 A 和字母 B
- 随机化并在页面加载时显示给定数量的 if 元素
- jQuery Rotator是按字母顺序排列的,我怎样才能在我的主页上随机化
- PHP 从现有代码中随机化文件列表数组