0

我将如何修改下面的 PHP 代码以输出目录中的所有文件,但将它们置于每次重新加载页面时都会更改的随机顺序?

if ($handle = opendir("files/slideshow/")) {
    while (false !== ($slide = readdir($handle))) {
        if ($slide != "." && $slide != "..") {
            echo "<img style=\"background:url('/files/slideshow/{$slide}');background-repeat:no-repeat;\" src=\"/files/images/i.png\"/>";
            $value = "1";
        }
    }
    closedir($handle);
}
4

1 回答 1

3

使用更短的方式与glob添加数组shuffle

$files = glob('files/slideshow/*'); // or even 'files/slideshow/*.png'
shuffle($files); 
foreach ($files as $slide)
    {
    echo "<img style=\"background:url('/files/slideshow/{$slide}');background-repeat:no-repeat;\" src=\"/files/images/i.png\"/>";
    $value = "1";
    }
于 2013-05-20T01:35:31.090 回答