SteveDahdah Posted December 9, 2013 Share Posted December 9, 2013 I have the following code: <? Header("content-type: application/x-javascript"); $pathstring=pathinfo($_SERVER['PHP_SELF']); $locationstring="http://" . $_SERVER['HTTP_HOST'].$pathstring['dirname'] . "/"; function returnimages($dirname=".") { $pattern="(\.jpg$)|(\.jpeg$)|(\.gif$)"; $files = array(); $mostRecent=0; $curimage=0; if($handle = opendir($dirname)) { while(false !== ($file = readdir($handle))){ if(eregi($pattern, $file)){ echo 'picsarray[' . $mostRecent .']="' . $file . '";'; $mostRecent++; } } closedir($handle); } return($files); } echo 'var locationstring="' . $locationstring . '";'; echo 'var picsarray=new Array();'; returnimages() ?> I want to sort the images by reverse because This code sort pictures like this: picsarray[0]="2013-11-29 11:42:11 pm.jpg";picsarray[1]="2013-11-29 11:58:24 pm.jpg";picsarray[2]="2013-11-30 12:13:36 am.jpg";picsarray[3]="2013-11-30 12:27:46 am.jpg" I want to sort them by reverse. Anyone can help me to fix this problem? Quote Link to comment https://forums.phpfreaks.com/topic/284650-php-images-sorting-by-reverse/ Share on other sites More sharing options...
Ch0cu3r Posted December 9, 2013 Share Posted December 9, 2013 Sort the picsarray in the javascript using picsarray.reverse(); Or sort the pictures first in PHP before generating the JavaScript picsarray function returnimages($dirname=".") { $pattern="~\.(jpe?g|gif)$~"; $files = array(); if($handle = opendir($dirname)) { while(false !== ($file = readdir($handle))){ if(preg_match($pattern, $file)){ $files[] = $file; } } closedir($handle); } // sort pics in reverse order rsort($files); // output images into javascript array foreach($files as $key => $pic) { echo "picsarray[$key] = '$pic'"; } } Quote Link to comment https://forums.phpfreaks.com/topic/284650-php-images-sorting-by-reverse/#findComment-1461778 Share on other sites More sharing options...
SteveDahdah Posted December 9, 2013 Author Share Posted December 9, 2013 The second code is working. But now i cant display images as a drop down menu in my html page. can you help me? Quote Link to comment https://forums.phpfreaks.com/topic/284650-php-images-sorting-by-reverse/#findComment-1461783 Share on other sites More sharing options...
Ch0cu3r Posted December 9, 2013 Share Posted December 9, 2013 Add a ; after '$pic' below echo "picsarray[$key] = '$pic';"; ^ add this This could be causing a JavaScript error, which then prevents the dropdown menu from displaying. Quote Link to comment https://forums.phpfreaks.com/topic/284650-php-images-sorting-by-reverse/#findComment-1461801 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.