Page 1 of 1

Order of images displayed...

Posted: Wed Nov 19, 2003 1:41 pm
by Mo
The following code goes through the "images/gage/" directory and looks for all image files. It then stores them in an array - $images[].

Code: Select all

<html> 
<body> 

<?php
$allowedExtensions = array('.jpg', '.jpeg', '.png', '.bmp'); 
$dir = "images/gage/";
$dh = opendir($dir);
$i=1;

while (($filename = readdir($dh)) !== false) {
   if (is_file($dir.$filename)) {
      $extension = strrchr($filename, '.');
      if (in_array($extension, $allowedExtensions)) {
      $image[$i]=$filename;
      $i++;
      }
   }
}
$i++;
closedir($dh); 
?>

<img src="<?= "$dir/$image[1]" ?>"><br>
<img src="<?= "$dir/$image[2]" ?>"><br> 
<img src="<?= "$dir/$image[3]" ?>"><br> 
<img src="<?= "$dir/$image[4]" ?>"><br> 
<img src="<?= "$dir/$image[5]" ?>"><br> 

 </body> 
</html>
I have 5 image files in the directory named gage_1.jpg thru gage_5.jpg.

I notice when I look at the code thru my web browser it is saying the order of the images displayed is:
gage_2.jpg
gage_3.jpg
gage_4.jpg
gage_5.jpg
gage_1.jpg

If gage_1.jpg is the first image in the directory why does the code store gage_2.jpg in the $image[1]"?

Any other comments on this code are welcome.

Posted: Wed Nov 19, 2003 1:56 pm
by JPlush76
try using the sort() function

check the manual for the details

Posted: Wed Nov 19, 2003 1:57 pm
by DuFF
Try sorting the array using array_multisort using SORT_DESC or SORT_ASC. I'm not sure why it does that.