Page 1 of 1
getting image for ajax slideshow
Posted: Tue Jul 06, 2010 9:45 pm
by manRay
I need a way to cycle through a set of images in a random order from a directory on my server.I figure there will be some ajax that changes the previous image which it gets from a php script that it called. Am I close? Anyway, does anybody know of way to do this? Thanks in advanced!
Re: getting image for ajax slideshow
Posted: Wed Jul 07, 2010 9:28 am
by Jade
You can use
scandir to get an array of files in the directory and then use PHP to randomly select one of those and feed it to your Ajax.
Re: getting image for ajax slideshow
Posted: Wed Jul 07, 2010 11:53 am
by Bind
PHP Iterator Classes are another alternative:
Code: Select all
<?php
$path = '/path/to/image/directory/';
$iterator = new RecursiveDirectoryIterator($path);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $filename)
{
if(preg_match('%\.(bmp|gif|jpe?g|png)$%i',$filename->getFilename()))
{
# configure your output here (delimination, headers, xml, plaintext, etc)
# test conditions-based output of /path/to/file.extension
echo $path.$filename->getFilename().'\r\n';
}
}
?>
Re: getting image for ajax slideshow
Posted: Wed Jul 07, 2010 2:23 pm
by manRay
I am using the scandir method, but my echo statement is giving the wrong output from what I wanted it to output.
Here is my code
Code: Select all
$tip = $_GET["tip"];
$OS = $_GET["os"];
if ($OS == "mac")
$gallery = scandir("../images/mac_tips");
else if ($OS == "win")
$gallery = scandir("../images/pc_tips");
if ($gallery)
echo "<img src='".$gallery[$tip] ."' />";
and my output is <img src>
Re: getting image for ajax slideshow
Posted: Wed Jul 07, 2010 2:48 pm
by Jade
Try doing print_r($gallery); to make sure you're getting something in your scandir array. You're either trying to access a file that doesn't exist or you have an empty array because the directory path is wrong.
Code: Select all
$tip = $_GET["tip"];
$OS = $_GET["os"];
if ($OS == "mac")
$gallery = scandir("../images/mac_tips");
else if ($OS == "win")
$gallery = scandir("../images/pc_tips");
if ($gallery)
echo "<img src='".$gallery[$tip] ."' />";
print_r($gallery);
Re: getting image for ajax slideshow
Posted: Thu Jul 08, 2010 3:51 pm
by manRay
I changed my code to
Code: Select all
$gallery = scandir("../images/gen_tips");
if ($gallery) {
unset($gallery[0]);
unset($gallery[1]);
$gallery = array_values($gallery);
echo "<img src='../images/gen_tips/".$gallery[$tip] ."' />";
}
but it is printing <img src="../images/gen_tips/"> and I know for sure there are files in the gen_tips directory, 2 to be exact! I unset the first two array items because they are . and .., which are not useful.
Re: getting image for ajax slideshow
Posted: Fri Jul 09, 2010 1:43 pm
by Jade
You need to put a blackslash after the directory name.
Code: Select all
$gallery = scandir("../images/gen_tips/");