简体   繁体   中英

php javascript autopopulated image gallery

I get the error message syntax error missing ; before statement at "var galleryarray=new Array();" . "\\n"; here is the php code

function returnimages($dirname=".") {
    $pattern="\.(jpg|jpeg|png|gif|bmp)$";
    $files = array();$curimage=0;
    if($handle = opendir($dirname)) {
        while(false !== ($file = readdir($handle))){
            if(eregi($pattern, $file)){
                echo 'galleryarray[' . $curimage .']=["' . $file . '"];' . "\n";
                $curimage++;
            }
        }
    closedir($handle);
    }
    return($files);
}
echo "var galleryarray=new Array();" . "\n";
returnimages();

and here is the javascript:

var galleryarray=new Array(); 
var curimg=0
function rotateimages(){
    document.getElementById("slideshow").setAttribute("src", "slideshow_images/"+galleryarray[curimg])
    curimg=(curimg<galleryarray.length-1)? curimg+1 : 0
}
window.onload=function(){
    setInterval("rotateimages()", 2500)
}

i just don't see my mistake any help with this problem would be appreciated

klein

Replace your below line:

echo "var galleryarray=new Array();" . "\n";

with the following line:

echo "<script>var galleryarray=new Array();</script>";

You are adding a JS code without script tag so you need to add this tag.

EDITED:

echo 'galleryarray[' . $curimage .']=["' . $file . '"];' . "\n";

You also have error in the above line replace it with the below line:

echo '<script>galleryarray["' . $curimage .'"]=["' . $file . '"];</script>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM