getting image for ajax slideshow

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

getting image for ajax slideshow

Post 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!
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: getting image for ajax slideshow

Post 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.
Bind
Forum Contributor
Posts: 102
Joined: Wed Feb 03, 2010 1:22 am

Re: getting image for ajax slideshow

Post 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';
            }
    }
?>
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: getting image for ajax slideshow

Post 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>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: getting image for ajax slideshow

Post 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);
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: getting image for ajax slideshow

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: getting image for ajax slideshow

Post by Jade »

You need to put a blackslash after the directory name.

Code: Select all

$gallery = scandir("../images/gen_tips/");
Post Reply