Note that there are some explanatory texts on larger screens.

plurals
  1. POproblem with php: read filenames, generate javascript and html
    primarykey
    data
    text
    <h2><strong>UPDATE</strong></h2> <p>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:</p> <pre><code>window.onload = photos(); </code></pre> <p>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????</p> <p><strong>END OF UPDATE</strong></p> <hr> <p>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!</p> <p>Firstly, here is the php/javascript(in getPhotos.php):</p> <pre><code>&lt;? 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&lt;galleryArray.length; i++){ if(galleryArray[i].indexOf("set1_")!=-1){ text1+= "&lt;a rel=\"gallery\" title=\"\" href=\"photos/"+galleryArray[i]+"\"&gt;\n&lt;img alt=\"\" src=\"photos/"+galleryArray[i]+"\" /&gt;\n&lt;/a&gt;\n" ; }else if(galleryArray[i].indexOf("set2_")!=-1){ text2+= "&lt;a rel=\"gallery\" title=\"\" href=\"photos/"+galleryArray[i]+"\"&gt;\n&lt;img alt=\"\" src=\"photos/"+galleryArray[i]+"\" /&gt;\n&lt;/a&gt;\n" ; }else if(galleryArray[i].indexOf("set3_")!=-1){ text3+= "&lt;a rel=\"gallery\" title=\"\" href=\"photos/"+galleryArray[i]+"\"&gt;\n&lt;img alt=\"\" src=\"photos/"+galleryArray[i]+"\" /&gt;\n&lt;/a&gt;\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); }'; ?&gt; </code></pre> <p>And this is the code in the mane page index.html:</p> <pre><code>&lt;script type="text/javascript" src="getPhotos.php"&gt;&lt;/script&gt;&lt;!--get photos from dir--&gt; </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload