Page 1 of 1

Order of directory contents

Posted: Tue Dec 16, 2008 2:14 pm
by psurrena
Does anyone know why the results are out of numeric order?

Code: Select all

$images="images/work/{$id}/"; 
$dir=opendir($images);
 
while($name=readdir($dir)){
    if (preg_match("/([0-9]+\.jpg)$/",$name)){
        $str=substr($name,0,-4);
        if($switch==FALSE){
            $active="class=\"active\"";
            $switch=TRUE;
        } else {
            $active="";
        } 
        $tags['gallery'].="<li {$active}><a href=\"{$images}{$str}.jpg\"><img src=\"{$images}{$str}t.jpg\" width=\"61\" height=\"50\" alt=\"{$value['project_name']}\" /></a></li>\n";
    }
}
returns

Code: Select all

<li class="active"><a href="images/work/118/9.jpg"><img src="images/work/118/9t.jpg" width="61" height="50" alt="Los Morros Master Plan" /></a></li>
<li ><a href="images/work/118/1.jpg"><img src="images/work/118/1t.jpg" width="61" height="50" alt="Los Morros Master Plan" /></a></li>
<li ><a href="images/work/118/2.jpg"><img src="images/work/118/2t.jpg" width="61" height="50" alt="Los Morros Master Plan" /></a></li>
<li ><a href="images/work/118/5.jpg"><img src="images/work/118/5t.jpg" width="61" height="50" alt="Los Morros Master Plan" /></a></li>
<li ><a href="images/work/118/6.jpg"><img src="images/work/118/6t.jpg" width="61" height="50" alt="Los Morros Master Plan" /></a></li>
<li ><a href="images/work/118/3.jpg"><img src="images/work/118/3t.jpg" width="61" height="50" alt="Los Morros Master Plan" /></a></li>
<li ><a href="images/work/118/7.jpg"><img src="images/work/118/7t.jpg" width="61" height="50" alt="Los Morros Master Plan" /></a></li>
<li ><a href="images/work/118/8.jpg"><img src="images/work/118/8t.jpg" width="61" height="50" alt="Los Morros Master Plan" /></a></li>
<li ><a href="images/work/118/4.jpg"><img src="images/work/118/4t.jpg" width="61" height="50" alt="Los Morros Master Plan" /></a></li>
 

Re: Order of directory contents

Posted: Tue Dec 16, 2008 2:30 pm
by John Cartwright
I'm not exactly sure the mechanics behind the order the files are retrieved, but I wouldn't expect it to be returned in alphanumeric order anyways. Simply apply a sort() function on your recordset if you require it sorted. I also have found glob() much easier to handle than opendir()/readdir(). One bonus to glob() is it will automatically sort for you unless you specify it not to.

Code: Select all

 
$imagepath = 'images/work/'.$id .'/';
$images = glob($imagepath .'*.jpg');
 
foreach ($images as $image) {
   $imagename = basename($image,'.jpg'); //strip extension
   if($switch==FALSE){
      $active="class=\"active\"";
      $switch=TRUE;
   } else {
      $active="";
   }
 
   $tags['gallery'] .= '<li '. $active .'><a href=""'. $imagepath . $imagename .'.jpg">';
   $tags['gallery'] .= '<img src="'. $imagepath . $imagename .'t.jpg" width="61" height="50" alt="'. $value['project_name'] .'" />';
   $tags['gallery'] .= '</a></li>' . "\n";
}
 

Re: Order of directory contents

Posted: Tue Dec 16, 2008 2:31 pm
by andyhoneycutt
readdir function
Description
string readdir ([ resource $dir_handle ] )

Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem

Re: Order of directory contents

Posted: Tue Dec 16, 2008 2:52 pm
by psurrena
never used glob() before, it does seem a bit simpler. Thank you!