I found this cool picture viewer. It has a drop down box that gets populated by the files in a specified directory. The only problem is, there is no sort. I have beat my brains out trying to figure out how to sort this. Any ideas? Thanks!

It is called a Dynamic PHP Picture Viewer.

http://www.javascriptkit.com/script/script2/phpviewer/

SAMPLE:

http://oberman.info/pics/

QUESTION:

How would I get the picture list in alpha order?

PHP CODE:

<?
Header("content-type: application/x-javascript");
$pathstring=pathinfo($SERVER['PHP_SELF']);
$locationstring="http://" . $
SERVER['HTTP_HOST'].$pathstring['dirname'] . "/";

function returnimages($dirname=".") {
$pattern="(.jpg$)|(.png$)|(.jpeg$)|(.gif$)";
$files = array();
$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){
echo 'picsarray[' . $curimage .']="' . $file . '";';
$curimage++;
}
}

closedir($handle);
}
return($files);
}

echo 'var locationstring="' . $locationstring . '";';
echo 'var picsarray=new Array();';
returnimages()
?>

    [man]asort/man

    Just get all the images into an array, and sort using the above function...

      Actually that script adds the name of the image files to the array automatically. So they don't need to be manually added. This is a nice script for quickly putting pictures on the web.

      The bad thing is it randomly populates the array.

      I tried using asort() but probably didn't do it right. Where does the arsort go? Thanks.

        after you completely populate the array, and before you display it....

          If you have PHP >=4.3 (4.3.3 on win32) you could use glob which returns an array in the first place (which sort() will work on ... actually glob() sorts by default but you can use natcaseort() & friends as well) ...

          PHP Code:
          <?
          Header("content-type: application/x-javascript");
          $pathstring=pathinfo($SERVER['PHP_SELF']);
          $locationstring="http://" . $
          SERVER['HTTP_HOST'].$pathstring['dirname'] . "/";

          function returnimages($dirname=".") {
          $yaks = glob("*.{jpg,gif,png,jpeg}",GLOB_BRACE);
          sort ($yaks);
          foreach($yaks as $k=>$v){
          $str .= $files[$file] = 'picsarray[' . $k .']="' . $v . '";'."\n";
          }
          return $str;
          }

          echo 'var locationstring="' . $locationstring . '";';
          echo 'var picsarray=new Array();';
          echo returnimages();
          ?>

          If you have an older version and need to use readdir you will have to put the filenames in an array & then iterate through them again.

          PHP Code:
          <?
          Header("content-type: application/x-javascript");
          $pathstring=pathinfo($SERVER['PHP_SELF']);
          $locationstring="http://" . $
          SERVER['HTTP_HOST'].$pathstring['dirname'] . "/";

          function returnimages($dirname=".") {
          $pattern="(.jpg$)|(.png$)|(.jpeg$)|(.gif$)";
          $files = array();
          if($handle = opendir($dirname)) {
          while(false !== ($file = readdir($handle))){
          if(eregi($pattern, $file)){
          $files[]=$file;
          }
          }
          closedir($handle);
          }
          sort ($files);
          foreach($files as $k=>$v){
          $str .= $files[$file] = 'picsarray[' . $k .']="' . $v . '";'."\n";
          }
          return $str;
          }

          echo 'var locationstring="' . $locationstring . '";';
          echo 'var picsarray=new Array();';
          echo returnimages();
          ?>

            Write a Reply...