Order of images displayed...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mo
Forum Newbie
Posts: 21
Joined: Thu Nov 06, 2003 7:21 am

Order of images displayed...

Post 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.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

try using the sort() function

check the manual for the details
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Try sorting the array using array_multisort using SORT_DESC or SORT_ASC. I'm not sure why it does that.
Post Reply