working image display PHP code but i'm having trouble getting it to sort...

Discussion in 'Programming' started by js09, May 5, 2007.

  1. #1
    On one of my sites i have a folder of images and it uses this code to display them:
      <?PHP
    function createGallery($pathToImages, $pathToThumbs)
    {
      $output = "<table cellspacing=\"0\" cellpadding=\"0\" width=\"90%\">\n";
      $output .= "<tr>\n";
      $dir = opendir($pathToThumbs);
      $counter = 0;
      while (false !== ($fname = readdir($dir)))
      {
        if ($fname != '.' && $fname != '..') 
        {
          $output .= "<td valign=\"middle\" align=\"center\" class=\"pictures\">\n
    	  				<a href=\"#\" onClick=\"viewImage('{$pathToImages}{$fname}')\">\n";
          $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />\n";
          $output .= "</a>\n</td>\n";
          $counter += 1;
          if ($counter % 4 == 0) {$output .= "</tr>\n<tr>";}
        }
      }
      closedir($dir);
      $output .= "</tr>\n";
      $output .= "</table>\n";
      $output .= "</body>\n";
      $output .= "</html>\n";
      echo $output;
    }
    createGallery("pictures/","pictures/thumbs/");
    ?>
    PHP:
    the problem is it sorts the images completely randomly. Id like to have the images be sorted by name or date added. what is the code to do this?

    thanks!
     
    js09, May 5, 2007 IP
  2. js09

    js09 Peon

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    bumppppppp
     
    js09, May 5, 2007 IP
  3. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #3
    Try this:

    
    <?PHP
    function createGallery($pathToImages, $pathToThumbs)
    {
      $files = array();
    
      $output = "<table cellspacing=\"0\" cellpadding=\"0\" width=\"90%\">\n";
      $output .= "<tr>\n";
      $dir = opendir($pathToThumbs);
      $counter = 0;
      while (false !== ($fname = readdir($dir)))
      {
        if ($fname != '.' && $fname != '..')
        {
           $files[] = $fname;
        }
      }
      closedir($dir);
      
      
      sort($files);
    
      foreach ($files as $fname)
      {
          $output .= "<td valign=\"middle\" align=\"center\" class=\"pictures\">\n
                  <a href=\"#\" onClick=\"viewImage('{$pathToImages}{$fname}')\">\n";
          $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />\n";
          $output .= "</a>\n</td>\n";
          $counter += 1;
          if ($counter % 4 == 0) {$output .= "</tr>\n<tr>";}
      }
    
      $output .= "</tr>\n";
      $output .= "</table>\n";
      $output .= "</body>\n";
      $output .= "</html>\n";
      echo $output;
    }
    createGallery("pictures/","pictures/thumbs/");
    ?>
    
    PHP:
     
    SoKickIt, May 5, 2007 IP
  4. js09

    js09 Peon

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    works great, thanks a ton!
     
    js09, May 6, 2007 IP