0

here's my code. But the read(): in the while loop does not seems to sort my result aphabetically. Is there a way to sort my result? tks Seby

 <?php
 $save_path_folder_images = '../simplegallery/users/'.$_SESSION['user_id'].'       /'.$_REQUEST['gal_folder'].'/thumbs/';
 $save_path_folder_images_full = '/simplegallery/users/'.$_SESSION['user_id'].'/'.$_REQUEST['gal_folder'].'/slides/';
$folder=dir($save_path_folder_images);

$counter = 1;
while($folderEntry=$folder->read())
{
    if($folderEntry!="." && $folderEntry!="..")
    {?>
         <div class="imgs" >
                <div class="thumb" >                        
                        <label for="img<?php echo $counter; ?>"><img class="img" src="<?php echo $save_path_folder_images.$folderEntry; ?>" /></label>                          
                    </div>
                <div class="inner">
                    <input type="checkbox" class="chk " id="img<?php echo $counter; ?>" name="img_name[]" value="<?php echo $save_path_folder_images_full.$folderEntry; ?>" />
                </div>
            </div>
        <?php
    $counter++;
    }
}
  ?>
seby
  • 15
  • 2
  • 8

2 Answers2

1

->read() does not attempt to sort the output, you could loop through read() once, pushing the output into an array. Then run it through natcasesort() (http://php.net/natcasesort) to sort the array, then print it out.

So two loops.

while($folderEntry=$folder->read())
{
  $fileList[]  = $folderEntry;
}
natcasesort($fileList);
foreach($fileList as $folderEntry)
{
  //your printing
}
preinheimer
  • 3,712
  • 20
  • 34
  • Hi sir, that's what I don't know how to do from what I have right now. Can you help me a little on it please? – seby May 25 '13 at 14:06
  • Yep, totally working sir. thank you very very much for your help. Have a nice day.. Seby – seby May 25 '13 at 14:31
0

Why don't you use DirectoryIterator?

$files = array();
$dir = new DirectoryIterator($save_path_folder_images);
foreach ($dir as $fileinfo) {     
   if ($fileinfo->isFile()) {
       $files[] = $fileinfo->getFilename();
   }
}

natcasesort($files);

check this thread as well Sorting files with DirectoryIterator

Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85