开发者

Implementing rand function

I have the following section of code. Is there anyway that I can use the php rand() function to randomly display items within the foreach?. I have tried google but I am getting confused how to implement it into this situation.

<?php
$dir = 'catalog/view/theme/default/gallery/';?>
<div id="Box">
<div id="slideShow">
<ul>
<?php foreach(glob($dir.'开发者_如何学JAVA*.jpg') as $file) : ?>
<li><img width="370" height="480" alt="" src="<?=$file?>"/></li>
<?php endforeach; ?>
</ul>
</div>
</div>


Assuming you want to display all of the images but in a random order, you could try something like this:

<?php
    $files = glob($dir . "*.jpg");
    shuffle($files);
    foreach($files as $file):
?>
<li><img width="370" height="480" alt="" src="<?= $file ?>" /></li>
<?php
    endforeach;
?>


I have used rand() in my SELECT query before using foreach like order by rand() it too worked.


You could read the files into an array and use array_rand

http://php.net/manual/en/function.array-rand.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜