PHP - How to search folder images per order -
i have following code search folder /images/ images , echo them. however, displays images random order everytime refresh page. images named 1, 2, 3, 4 , on. way make last number (ex: 4) first 1 being displayed , on?
<?php $dirname = "images"; $images = scandir($dirname); shuffle($images); $ignore = array(".", ".."); foreach($images $curimg){ if(!in_array($curimg, $ignore)) { echo "<img src=\"". $dirname . '/' . $curimg ."\">" ; } } ?>
thanks in advance.
this due shuffle. randomizing array. let me introduce to: http://php.net/manual/en/function.array-reverse.php
<?php $dirname = "images"; $images = scandir($dirname); $images = arsort(array_reverse($images, true)); $ignore = array(".", ".."); foreach($images $curimg){ if(!in_array($curimg, $ignore)) { echo "<img src=\"". $dirname . '/' . $curimg ."\">" ; } } ?>
update:
$dirname = "images"; $images = scandir($dirname); sort($images,sort_numeric); krsort($images); $ignore = array(".", ".."); foreach($images $curimg){ if(!in_array($curimg, $ignore)) { echo "<img src=\"". $dirname . '/' . $curimg ."\"> \n" ; } }
what have been working with:
without sort();
, krsort();
return:
<img src="images/1.png"> <img src="images/10.png"> <img src="images/11.png"> <img src="images/2.png"> <img src="images/3.png"> <img src="images/4.png"> <img src="images/5.png"> <img src="images/6.png"> <img src="images/7.png"> <img src="images/8.png"> <img src="images/9.png">
with krsort , sort.. return:
<img src="images/11.png"> <img src="images/10.png"> <img src="images/9.png"> <img src="images/8.png"> <img src="images/7.png"> <img src="images/6.png"> <img src="images/5.png"> <img src="images/4.png"> <img src="images/3.png"> <img src="images/2.png"> <img src="images/1.png">
which presume looking for.
Comments
Post a Comment