I’m not sure if this should go in the PHP forum but as the issue concerns Javascript I’ve put it here. I am experimenting with a script that uses PHP to populate an array with the contents of a directory (a list of image files). This is the PHP:
[code]
<?
//PHP SCRIPT: getimages.php
Header(“content-type: application/x-javascript”);
//This function gets the file names of all images in the current directory
//and ouputs 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);
}
return($files);
}
echo ‘var galleryarray=new Array();’; //Define array in JavaScript
returnimages() //Output the array elements containing the image file names
?>
and the javascript:
[code]
<HTML>
<HEAD>
<script src=”http://www.wthrman.com/maps/surface/200001/getimages.php”>
//This is the directory where the images are eg www.wthrman.com/maps/surface/ etc..put the php in there
</script>
<script type=”text/javascript”>
var curimg=0
function rotateimages(){
document.getElementById(“slideshow”).setAttribute(“src”, “http://www.wthrman.com/maps/surface/200001/”+galleryarray[curimg])//Change the directory here too ie “…” + galleryarray[curimg]
curimg=(curimg<galleryarray.length-1)? curimg+1 : 0
}
window.onload=function(){
setInterval(“rotateimages()”, 2500)
}
</script>
</HEAD>
<BODY>
<div style=”width: 680px; height: 400px”>
<img id=”slideshow” src=”2000010100.png”/>
</div>
</BODY>
</HTML>
As it stands, the scripts automatically run a slideshow of all the images in the directory, when the .htm file is opened. However I don’t want to automatically see all the files, but only a certain range of them. The images have date formats as in YYYYMMDDHH (HH is for hours at 6-hourly intervals ie 00,06,12,18).
I’m sure this can be done with 2 sets of option boxes that select a first and last date based on the above file format…the idea is to view all the images in between the two dates selected (there will be a box of options for first and last year, month, day and hour..this is the only way I know how to specify the dates).
I’ve no problem with setting up the option boxes, but apart from having to bypass the automatic ‘onload’ so the user can access them, I don’t know what I then should do to get only the images between the two dates, into the array, assuming it would function as it does with all the images, as outlined above.
The foregoing is all a bit long-winded but I think there’s got to be a simple solution.
Could someone please help?
Thank you