tiny Posted September 26, 2009 Share Posted September 26, 2009 I have made a code that will retrieve all photos from a folder. It's working fine. The only problem is, the images are not arrange properly. My images are named this way: gallery1.jpg gallery2.jpg gallery3.jpg gallery4.jpg gallery5jpg gallery6.jpg gallery7.jpg gallery8.jpg gallery9.jpg gallery10.jpg gallery11.jpg gallery12.jpg so when i have the images displayed, instead of being arranged that way, they appear in this arrangement: gallery1.jpg gallery10.jpg gallery11.jpg gallery12.jpg gallery2.jpg gallery3.jpg gallery4.jpg gallery5jpg gallery6.jpg gallery7.jpg gallery8.jpg gallery9.jpg Here's the code: <?php $path = "..".$_SERVER['REQUEST_URI']."images/stories/mygallery/thumbs/"; $dir_handle = @opendir($path) or die("Unable to open folder"); while (false !== ($file = readdir($dir_handle))) { if(!in_array($file, array("index.php","..","."))){ //explode to include only images $explodefile = explode(".", $file); if(in_array($explodefile[1], array("jpg","png","gif"))){ $fileList[] = $file; } } } closedir($dir_handle); $i = 0; while($fileList[$i] != ""){ echo "<li><a href='http://localhost/mysite/galleryview'><img src='".$_SERVER['REQUEST_URI']."images/stories/mygallery/thumbs/{$fileList[$i]}'></a></li>"; $i++; } ?> How can I arrange the images so they appear by their filenames accordingly (following their numbers) like this: gallery1.jpg gallery2.jpg gallery3.jpg gallery4.jpg gallery5jpg gallery6.jpg gallery7.jpg gallery8.jpg gallery9.jpg gallery10.jpg gallery11.jpg gallery12.jpg Thank you in advance! Quote Link to comment https://forums.phpfreaks.com/topic/175613-solved-how-do-i-arrange-my-photos-by-their-file-names/ Share on other sites More sharing options...
Daniel0 Posted September 26, 2009 Share Posted September 26, 2009 Use natsort. By the way, there were ordered correctly before, in a sense. The standard way of sorting string a is using lexicographic ordering and a . (a period) comes before a 0 in ASCII. A period is 46 and a zero is 48 in ASCII. Quote Link to comment https://forums.phpfreaks.com/topic/175613-solved-how-do-i-arrange-my-photos-by-their-file-names/#findComment-925384 Share on other sites More sharing options...
tiny Posted September 26, 2009 Author Share Posted September 26, 2009 Thank you for your reply. I read about natsort and tried to put it on my code: What I changed on the last part of my code was this: $i = 0; while($fileList[$i] != ""){ $i++; } natsort($fileList); print_r($fileList); and the result was something like this: Array ( [0] => gallery1.jpg [4] => gallery2.jpg [5] => gallery3.jpg [6] => gallery4.jpg [7] => gallery5.jpg [8] => gallery6.jpg [9] => gallery7.jpg [10] => gallery8.jpg [11] => gallery9.jpg [1] => gallery10.jpg [2] => gallery11.jpg [3] => gallery12.jpg ) it did arrange my images. but i'm not quite sure how i'm supposedly going to slice that result so that I can display each images like what i did on my previous code. Quote Link to comment https://forums.phpfreaks.com/topic/175613-solved-how-do-i-arrange-my-photos-by-their-file-names/#findComment-925418 Share on other sites More sharing options...
tiny Posted October 2, 2009 Author Share Posted October 2, 2009 Hi, I am still having trouble sorting my gallery. I tried to use natsort(); and I have this code: $path = "/mypath"; $images=array(); $dh = @opendir($path) or die("Unable to open $path"); echo "Directory Listing of $path<br/>"; $i=0; while($f = readdir($dh)) { if(is_dir($f)) { continue; } else if($f != '.' && $f != '..') { //echo "<a href='$path/$file'>$file</a><br/>"; $images[$i]=$f; $i++; } } natsort($images); for($i=0; $i<sizeof($images); $i++) { echo "<a href=".chr(34).$path.$images[$i].chr(34).">".$images[$i]."</a><br/>"; } closedir($dh); the result still comes up this way.. gallery1.jpg gallery10.jpg gallery11.jpg gallery12.jpg gallery2.jpg gallery3.jpg gallery4.jpg gallery5.jpg gallery6.jpg gallery7.jpg gallery8.jpg gallery9.jpg what should i do? please help. thank you Quote Link to comment https://forums.phpfreaks.com/topic/175613-solved-how-do-i-arrange-my-photos-by-their-file-names/#findComment-928961 Share on other sites More sharing options...
cags Posted October 2, 2009 Share Posted October 2, 2009 The array is in order, it's the way your outputting it. When natsort moves a value in the array it also moves the associated key. It makes more sense when you take into account the fact that PHP supports associative arrays. Once the array has been sorted the key is considered the name of that item in the array, rather than an actual index of it's actual numeric position. I'm not sure if that made it any clearer or more confusing, but if you use this... <?php echo '<pre>'; print_r($images); echo '</pre>'; ?> ...you should see why your for loop is printing in the wrong order. As it happens the solution is very simple. <?php // replace for($i=0; $i<sizeof($images); $i++) { echo "<a href=".chr(34).$path.$images[$i].chr(34).">".$images[$i]."</a><br/>"; } // with something like foreach($images as $image) { echo '<a href="' . $path . $image . '">' . $image . '</a><br/>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/175613-solved-how-do-i-arrange-my-photos-by-their-file-names/#findComment-928972 Share on other sites More sharing options...
thebadbad Posted October 2, 2009 Share Posted October 2, 2009 glob() is less cluttering: <?php $path = '/mypath'; $images = glob($path . '/*.{jpg,jpeg,png,gif}', GLOB_BRACE); natsort($images); echo "Directory Listing of images in $path:<br />"; foreach ($images as $imagepath) { $name = basename($imagepath); echo "<br /><a href=\"$imagepath\">$name</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/175613-solved-how-do-i-arrange-my-photos-by-their-file-names/#findComment-928975 Share on other sites More sharing options...
tiny Posted October 2, 2009 Author Share Posted October 2, 2009 oh, now I understand. Thank you so much. It finally worked Quote Link to comment https://forums.phpfreaks.com/topic/175613-solved-how-do-i-arrange-my-photos-by-their-file-names/#findComment-929020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.