How to load another image randomly on click in php?

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
sbaker
Forum Newbie
Posts: 12
Joined: Wed Jul 20, 2011 12:47 pm

How to load another image randomly on click in php?

Post by sbaker »

Hi, I'm trying to load another image randomly that is stored in the images folder - when clicking on the image. A good example is this http://qikr.co/6hv56 . When you click on the image - you will notice that it loads another image randomly.

I'm not sure if it's javascript. Does anyone here have any experience with this? Thanks =)
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Re: How to load another image randomly on click in php?

Post by Corvin »

Code: Select all

<?php
$imageFolder = 'images';

// Get file names
// (do that manually if you want to save performance: $images[] = 'image1.png'; ...)
$images = array();
$dirh = opendir($imageFolder);
while( $file = readdir($dirh) ) {
    if( $file != "." && $file != ".." ) {
        $images[] = $file;
    }
}

$imageFileName = $images[rand(0, count($images)-1)];

echo '<img src="' . $imageFolder . '/' . $imageFileName . '" alt="" />';
sbaker
Forum Newbie
Posts: 12
Joined: Wed Jul 20, 2011 12:47 pm

Re: How to load another image randomly on click in php?

Post by sbaker »

Thanks Corvin. I tried the code, unfortunately it's not working for me. I inserted the code exactly as shown above. Do I have to insert anything in the code?

As of now only one image shows up, but I am not able to click on the image to see another images that is stored in the folder.

I want it to go through all the images in the folder without having to manually add each image in the code.
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Re: How to load another image randomly on click in php?

Post by Corvin »

Link the image to your script.

Code: Select all

echo '<a href=""><img src="' . $imageFolder . '/' . $imageFileName . '" alt="" /></a>';
sbaker
Forum Newbie
Posts: 12
Joined: Wed Jul 20, 2011 12:47 pm

Re: How to load another image randomly on click in php?

Post by sbaker »

I linked the image, however now it keeps going to the same image, rather than randomly selecting new images that is stored in the images folder.

Here is what I did:

Code: Select all

<?php
$imageFolder = 'images';

$images = array();
$dirh = opendir($imageFolder);
while( $file = readdir($dirh) ) {
    if( $file != "." && $file != ".." ) {
        $images[] = $file;
    }
}

$imageFileName = $images[rand(0, count($images)-1)];

echo '<a href="image-22.jpg"><img src="' . $imageFolder . '/' . $imageFileName . '" alt="" /></a>';
Last edited by Benjamin on Wed Jul 27, 2011 2:40 am, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Re: How to load another image randomly on click in php?

Post by Corvin »

Code: Select all

echo '<a href="SCRIPTNAME.PHP"><img src="' . $imageFolder . '/' . $imageFileName . '" alt="" /></a>'
sbaker
Forum Newbie
Posts: 12
Joined: Wed Jul 20, 2011 12:47 pm

Re: How to load another image randomly on click in php?

Post by sbaker »

Strange but when clicking on the image now it returns:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Here is what I did:

Code: Select all

<?php
$imageFolder = 'images';

$images = array();
$dirh = opendir($imageFolder);
while( $file = readdir($dirh) ) {
if( $file != "." && $file != ".." ) {
$images[] = $file;
}
}

$imageFileName = $images[rand(0, count($images)-1)];

echo '<a href="page-with-script.php"><img src="' . $imageFolder . '/' . $imageFileName . '" alt="" /></a>'
In what folder should I save the page-with-script.php file? I'm new to php, any help is grealty appreciated!
Last edited by Benjamin on Wed Jul 27, 2011 2:41 am, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
sbaker
Forum Newbie
Posts: 12
Joined: Wed Jul 20, 2011 12:47 pm

Re: How to load another image randomly on click in php?

Post by sbaker »

Corvin wrote:

Code: Select all

<?php
$imageFolder = 'images';

// Get file names
// (do that manually if you want to save performance: $images[] = 'image1.png'; ...)
$images = array();
$dirh = opendir($imageFolder);
while( $file = readdir($dirh) ) {
    if( $file != "." && $file != ".." ) {
        $images[] = $file;
    }
}

$imageFileName = $images[rand(0, count($images)-1)];

echo '<img src="' . $imageFolder . '/' . $imageFileName . '" alt="" />';
I got it to work! The only problem now is that I have over 300 images in folder, but when clicking on the image it is repeatingly showing the same first 20 images. Is there anyway it can randomize and go through all the images in the folder?

Perhaps list all the images into an array and save it in a session, shuffle this array and loop through the array, that would allow to never view the same images anymore. Does anyone here know how to code it?
Post Reply