0

UPDATE

Hello again. I found myself with a new problem. The 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.

14
  • When you say "it doesn't work", you need to be a little more specific. Does the page produce any output? If so, then where does it get to before it stops? If you see only a blank page, then that indicates a syntax error somewhere. Commented Mar 19, 2011 at 17:45
  • Hi, what do you mean by "doesn't work"? Is your galleryArray empty or are text1, text2, text3 empty? Commented Mar 19, 2011 at 17:45
  • You can also make sure that PHP is showing all errors by including these lines of code. Commented Mar 19, 2011 at 17:45
  • @strauberry Great minds and all that! Commented Mar 19, 2011 at 17:46
  • i want an answer without making the wright question....X_X sorry =) When I say doesn't I mean it produces nothing at all! Viewing the page source after i run it in a server and I can stil see all the php code (i think i shouldn't be able to see it) as i gave it above and no results. i have put in the folder photos with the wright name but it returns nothing. It's like it doesn't run at all. Commented Mar 19, 2011 at 17:49

2 Answers 2

1

; 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))){

2
  • @Michael : I edited my answer. Let me know it helps you or not. Commented Mar 19, 2011 at 18:06
  • i changed it but now it shows some other errors which from what i get they have to do with file names and the extensions i say it should look for... Commented Mar 19, 2011 at 18:10
0

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

1
  • thanx for the advice. anything more i learn can only help me! Commented Mar 19, 2011 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.