1

I've modified and cleaned this PHP script that someone else wrote. Locally on my WAMP server it lists the images alphabetically (they're all named 001.jpg ~ 110.jpg) however on the live LAMP server I think they're organized by date modified...whatever it is it's not by file name. They're all JPEG images so I'm not worried about arranging by type.

So, how do I modify this script to list images alphabetically please?

function getPictures()
{
 global $page, $per_page, $has_previous, $has_next;

 if ($handle = opendir('tour/'))
 {
  $lightbox = rand();
  echo '<ul id="pictures">';

  $count = 0;
  $skip = $page * $per_page;

  if ($skip != 0 ) {$has_previous = true;}

  while ($count < $skip && ($file = readdir($handle)) !== false )
  {
   if (!is_dir($file) && ($type = getPictureType($file)) != '' ) {$count++;}
  }

  $count = 0;

  while ( $count < $per_page && ($file = readdir($handle)) !== false )
  {
   if (!is_dir($file) && ($type = getPictureType($file)) != '' )
   {
    if (!is_dir('thumbs/')) {mkdir('thumbs/');}
    if (!file_exists('thumbs/'.$file)) {makeThumb('tour/'.$file,$type );}

    echo '<li><a href="tour/'.$file.'" rel="lightbox['.$lightbox.']">';
    echo '<img src="thumbs/'.$file.'" alt="" />';
    echo '</a></li>';
    $count++;
   }
  }
  echo '</ul>';

  while (($file = readdir($handle)) !== false)
  {
   if (!is_dir($file) && ($type = getPictureType($file)) != '' )
   {
    $has_next = true;
    break;
   }
  }
 }
}
John
  • 1
  • 13
  • 98
  • 177
  • 1
    Put the filenames in an array, sort that and output the list then. – Niko Mar 09 '12 at 20:22
  • possible duplicate of [Sort and display directory list alphabetically using opendir() in php](http://stackoverflow.com/questions/884974/sort-and-display-directory-list-alphabetically-using-opendir-in-php) – j08691 Mar 09 '12 at 20:24
  • I'm thinking array and this isn't a dupe of that question. – John Mar 09 '12 at 20:29
  • This is the cleaned-up version? ;-) – cmbuckley Mar 09 '12 at 21:42
  • @cbuckley I removed unnecessary spaces, put brackets at the beginning of the line, put brackets where they were missing altogether (!!!!), there was a missing isset (actually I think outside of this function)...it wasn't as badly formatted as some PHP though it was still a bit of a mess. I've posted a working answer below. – John Mar 09 '12 at 22:11

2 Answers2

2

Instead of using readdir, you can use scandir, which sorts alphabetically by default.

By default, the sorted order is alphabetical in ascending order. If the optional sorting_order is set to SCANDIR_SORT_DESCENDING, then the sort order is alphabetical in descending order. If it is set to SCANDIR_SORT_NONE then the result is unsorted.

Keep in mind, scandir returns an array of filenames, while readdir returns a single entry name.

Alternatively, you can read in your filenames into an array, and sort it using natsort.

// Orders alphanumeric strings in the way a human being would
natsort($arr);

Array
(
    [3] => img1.png
    [2] => img2.png
    [1] => img10.png
    [0] => img12.png
)
Josh
  • 8,082
  • 5
  • 43
  • 41
0

Looks like a "lightbox" function, if so here is the full modified version of the function I posted above...

function getPictures()
{
 if ($handle = opendir('tour/'))
 {
  global $page, $per_page, $has_previous, $has_next;
  $lightbox = rand();
  echo '<ul id="pictures">';
  $count = 0;
  $skip = $page * $per_page;

  $file = scandir('tour/');
  $images = array();

  foreach ($file as $key => $value)
  {
   if (!is_dir('tour/'.$value) && ($type = getPictureType('tour/'.$value)) != '' )
   {
    array_push($images,$value);
   }
  }

  natsort($images);

  $count = 0;
  $start = $per_page*$page;
  $end = $start+$per_page - 1;

  foreach ($images as $key => $value)
  {
   if ($key>=$start && $key<=$end)
   {
    echo '<li><a href="tour/'.$value.'" rel="lightbox['.$lightbox.']"><img src="thumbs/'.$value.'" alt="" /></a></li>';
    $count++;
   }
  }
  $not_first = $end+1;
  if ($key>$end) {$has_next = true;}
  if ($not_first!=$per_page) {$has_previous = true;}

  echo '</ul>';
 }
}
John
  • 1
  • 13
  • 98
  • 177