Getting image array from PHP script

Discussion in 'JavaScript' started by saturn100, Oct 21, 2013.

  1. #1
    OK I am having issues populating a JS image gallery from a PHP script
    The PHP script creates an array on images found in a folder
    the script is here
    <?
    //PHP SCRIPT: getimages.php
    Header("content-type: application/x-javascript");
    
    //This function gets the file names of all images in the current directory
    //and ouputs them as a JavaScript array
    function returnimages($dirname=".") {
    $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
    $files = array();
    $curimage=0;
    if($handle = opendir($dirname)) {
    while(false !== ($file = readdir($handle))){
    if(eregi($pattern, $file)){ //if this file is a valid image
    //Output it as a JavaScript array element
    echo 'galleryarray['.$curimage.']="'.$file .'";';
    $curimage++;
    }
    }
    
    closedir($handle);
    }
    return($files);
    }
    
    echo 'var galleryarray=new Array();'; //Define array in JavaScript
    returnimages() //Output the array elements containing the image file names
    ?> 
    PHP:
    I need to input that array into my java script file
    which is

    var firstreel=new reelslideshow({
        wrapperid: "myreel", //ID of blank DIV on page to house Slideshow
        dimensions: [300, 200], //width/height of gallery in pixels. Should reflect dimensions of largest image
        imagearray:
       
    [
            ["image1"],
            ["image2"],
            ["image3"],
            ["image3"] 
        ],
        displaymode: {type:'auto', pause:2000, cycles:5, pauseonmouseover:true},
        orientation: "h", //Valid values: "h" or "v"
        persist: true, //remember last viewed slide and recall within same session?
        slideduration: 300 //transition duration (milliseconds)
    })
    
    Code (markup):
    I have the code working that reads the php file and changes images it is here

    <script src="pics/showimages.php"></script>
    
    <script type="text/javascript">
    
    var curimg=0
    function rotateimages(){
    document.getElementById("slideshow").setAttribute("src", "pics/"+galleryarray[curimg])
    curimg=(curimg<galleryarray.length-1)? curimg+1 : 0
    }
    
    window.onload=function(){
    setInterval("rotateimages()", 2500)
    }
    </script>
    
    <div style="width: 170px; height: 160px">
    <img id="slideshow" src="pics/bear.gif" />
    </div>
    HTML:
    but I need to input it into the array into the slide show
    Hope I explained myself OK and thanks for your help
     
    saturn100, Oct 21, 2013 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    540
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Usually, a good gallery would also do the slideshow.
    But it seems that your reelslideshow() object doesn't, does it.
    And that's what rotateimages() is here for?
    Could you share a link here to see this in action?
     
    hdewantara, Oct 21, 2013 IP
  3. jpmalloy

    jpmalloy Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    do a Ajax get request to your PHP file and output as JSON array
     
    jpmalloy, Oct 21, 2013 IP