/    Sign up×
Community /Pin to ProfileBookmark

Javascript array with PHP

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
?>
[/code]

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>
[/code]

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

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorDec 12.2008 — try to isolate the javascript problem from the PHP problem. PHP code is not relevant for javascript, as they don't interfere. Post only the HTML and javascript generated code.
×

Success!

Help @wthrman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...