开发者

problem with php: read filenames, generate javascript and html

开发者 https://www.devze.com 2023-02-19 15:43 出处:网络
UPDATE Hello again. I found myself with a new problem. T开发者_如何学运维he php code worked perfectly on my PC (wamp server) but i\'ve now uploaded it on a free webhost server and while the php part

UPDATE

Hello again. I found myself with a new problem. T开发者_如何学运维he php code worked perfectly on my PC (wamp server) but i've now uploaded it on a free webhost server and while the php part runs perfectly (it produces the array) the javascript function itself doesn't work cause there are no photos in the website when it's loaded. I tried to test it by putting in the function's first line an alert to see if it runs but never showed up. I think that the server for some reason doesn't realise that it is a javascript function because i also had in the getphotos.php this:

window.onload = photos();

which appart from starting the photos function, shows a text. When i moved that line in js file and put the show text line first, it run showing the text but still no photos. What do you think????

END OF UPDATE


Hello to everyone. I am building a website that shows some photos. I want the site to automatically generate the html code that shows the photos by reading the file names in the photo folder, but i need also to use javascript. So I found through the web a solution with php generating javascript which than generates the html code I want and I think this is what I need. But... it doesn't work X_X. So I need someone's help!

Firstly, here is the php/javascript(in getPhotos.php):

<?
header("content-type: text/javascript");

//This function gets the file names of all images in the current directory
//and ouputs them as a JavaScript array
function returnImages() {
    $pattern="(*.jpg)|(*.png)|(*.jpeg)|(*.gif)"; //valid image extensions
    $files = array();
    $curimage=0;
    if($handle = opendir('/photos/')) {
        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);
}

//here starts the javascript function
echo 'window.onload = photos;
    function photos(){
    var i;
    var text1 = "";
    var text2 = "";
    var text3 = "";
    var galleryArray=new Array();'; //Define array in JavaScript
returnImages(); //Output the array elements containing the image file names

//short the images in three sets depending on their names and produce the code
echo 'for(i=0; i<galleryArray.length; i++){
    if(galleryArray[i].indexOf("set1_")!=-1){
        text1+= "<a rel=\"gallery\" title=\"\" href=\"photos/"+galleryArray[i]+"\">\n<img alt=\"\" src=\"photos/"+galleryArray[i]+"\" />\n</a>\n" ;
    }else if(galleryArray[i].indexOf("set2_")!=-1){
        text2+= "<a rel=\"gallery\" title=\"\" href=\"photos/"+galleryArray[i]+"\">\n<img alt=\"\" src=\"photos/"+galleryArray[i]+"\" />\n</a>\n" ;
    }else if(galleryArray[i].indexOf("set3_")!=-1){
        text3+= "<a rel=\"gallery\" title=\"\" href=\"photos/"+galleryArray[i]+"\">\n<img alt=\"\" src=\"photos/"+galleryArray[i]+"\" />\n</a>\n" ;
    }
}';

//create text nodes and put them in the correct div
echo 'var code1 = document.createTextNode(text1);
    var code2 = document.createTextNode(text2);
    var code3 = document.createTextNode(text3);
    document.getElementById("galleryBox1").appendChild(code1);
    document.getElementById("galleryBox2").appendChild(code2);
    document.getElementById("galleryBox3").appendChild(code3);
}';

?> 

And this is the code in the mane page index.html:

<script type="text/javascript" src="getPhotos.php"></script><!--get photos from dir-->

This is it, and it doesn't work! I know I ask to much by just giving all the code and asking for help but i can't even think what's wrong, let alone how to fix it.... So please, if you have any idea it would be great.


; after returnImages() is missing.

This function (readdir) may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

http://php.net/manual/en/function.readdir.php

So try to use while(false != ($file = readdir($handle))){ or while(FALSE !== ($file = readdir($handle))){


Your regular expression is wrong, for one. You need to look at a regex tutorial, like this one.

0

精彩评论

暂无评论...
验证码 换一张
取 消