title: Dynamically select images for slide show using javascript
body: <p>I am using the following code to select images dynamically from a folder then display them in slide show.. but this code selects only first image... please guide me where is the error...</p>
  
  <p>PHP file is: named: test.php</p>
  
  <pre><code>&lt;?php
   header("content-type: application/x-javascript");
      function returnimages($dirname=".") {
          $files = array();
          $curimage = 0;
           //valid image extensions
          $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)";
          if($handle = opendir($dirname)) {
              while(false !== ($file = readdir($handle))) {
                  if(eregi($pattern, $file)){
                      echo 'galleryarray[' . $curimage . '] = "' . $file . '";';
                      $curimage++;
                  }
              }
             closedir($handle);
          }
          return($files);
      }
      //Define array in JavaScript returnimages()
      //Output the array elements containing the image file names
      echo 'var galleryarray = new Array();';
  ?&gt;
  </code></pre>
  
  <p>Html code is: </p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;&lt;/title&gt;
          &lt;script src="test.php"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;
              var galleryarray = returnimages();
              var curimg = 0;
              function rotateimages(){
                  //  var imagesDirectory = "pics/" + galleryarray[curimg];
                  var imagesDirectory =galleryarray[curimg];
                  document.getElementById("slideshow").setAttribute("src", imagesDirectory)
                  curimg = (curimg &lt; galleryarray.length - 1) ? curimg + 1 : 0
              }
              window.onload = function(){
                  setInterval("rotateimages()", 2500)
              }
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;img width="468" height="312" id="slideshow" src="Slide1.jpg"&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>