Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Image Slideshow

         

sm_79

10:49 am on Apr 14, 2007 (gmt 0)

10+ Year Member



Hi all,
I am new to this forum. I am stuck with a problem and I am hoping that you guys will be able to help me out.
I need to create an image slide show using HTML/Javascript.
I am using some scripts available [ on the internet ]. I am putting a snippet of the script below:

CODE:

<script type="text/javascript">
......
......

// array to specify images and optional links. At least 4
// If Link is not needed keep it ""

Book_Image_Sources=new Array(
"photo1.jpg","http://www.example.com",
"photo2.jpg","http://www.example.com",
"photo3.jpg","", //this slide isn't linked
"photo4.jpg","http://www.example.com"
);
..........
..........
</script>

<div id="Book" style="position:relative">
<img src="placeholder.gif" width="144" height="227">
</div>

-----------------------------------------------------------------

This above code is inserted in BODY section of the page where slideshow will appear.
An onload event handler is called from inside the <BODY> tag itself:
<body onload="ImageBook()">

Now here's my issue:
As you can see this script uses a statically defined array(Book_Image_Sources). But I want to use dynamic array i.e., I want to use PHP to read a particular directory containing the images, create the array with the image list and use that image array in the script to display the slide show.
Please help me out. Thanx in advance.

[edited by: coopster at 2:47 am (utc) on April 16, 2007]
[edit reason] no urls please TOS [webmasterworld.com] [/edit]

cameraman

2:22 am on Apr 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, sm_79!

Place this all as one lump where you've got the current declaration of Book_Image_Sources.


<?php
// this is the path to the images as seen by a browser
$htmlbase = "http://example.com/images/";
// this would be the directory path, something like home/yourusername/public_html/images
$hdl = opendir('path/to/images');
$imgs = '';
while($filename = readdir($hdl))
$imgs .= '"' . $htmlbase . $filename . '"' . ',"",';
$imgs = substr($filename,0,-1);
?>
Book_Image_Sources=new Array(<?php echo $imgs;?>);

Depending on how that script is written you may not need $htmlbase - if you specify it elsewhere in the script, for example. If so, change the line:
$imgs .= '"' . $htmlbase . $filename . '"' . ',"",';
to
$imgs .= '"' . $filename . '"' . ',"",';

and you can remove its declaration line (or leave it, it wouldn't do any harm).

sm_79

6:24 am on Apr 16, 2007 (gmt 0)

10+ Year Member



@ cameraman, Thanx a ton!