getting image for ajax slideshow
Moderator: General Moderators
getting image for ajax slideshow
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
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
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
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
and my output is <img src>
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] ."' />";Re: getting image for ajax slideshow
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
I changed my code to
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.
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] ."' />";
}Re: getting image for ajax slideshow
You need to put a blackslash after the directory name.
Code: Select all
$gallery = scandir("../images/gen_tips/");