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
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » Tue Dec 16, 2008 2:14 pm
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>
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Tue Dec 16, 2008 2:30 pm
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";
}
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Tue Dec 16, 2008 2:31 pm
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
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » Tue Dec 16, 2008 2:52 pm
never used glob() before, it does seem a bit simpler. Thank you!