I have seen a number of questions similar to this, but none that completely answered my question in a manner I understood.
I am building a website in which I would like to have a specified folder from which to retrieve all images (these could change from day to day, so I can't have it based on the file name, but rather the file path to that folder) and display them on the page. As of right now, I've seen a number of examples of doing something like this using PHP, Javascript, Ajax.... Lots.
However, I'm still having trouble implementing these examples or others' ideas into my own code. Below is a couple pieces of code that I've been trying to make work; I pulled these from various examples and ATTEMPTED to customize it for my needs, but have been unable to get a working HTML showing in my browser (Google Chrome).
Here's my PHP script:
<?
//PHP SCRIPT: getimages.php
Header("content-type: application/x-javascript");
//This function gets the file names of all images in the current directory
//and outputs 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);
}
for(int i = 0; i < 5; i++)
{
printf($files(i))
}
return($files);
}
echo 'var galleryarray=new Array();'; //Define array in JavaScript
returnimages() //Output the array elements containing the image file names
?>
And here is my HTML file:
<script src="http://www.javascriptkit.com/javatutors/phpslide.images.php"</script>
<script type="text/javascript">
var curimg=0
function rotateimages(){
document.getElementById("slideshow").setAttribute("src", "phpslide/"+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>
Can someone either walk me through what I'm doing wrong in these pieces of code, or provide me an idea of how to do what I want here? If you do answer, explanations would be appreciated to :) Still in the learning process here!