How to load another image randomly on click in php?
Moderator: General Moderators
How to load another image randomly on click in php?
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 =)
I'm not sure if it's javascript. Does anyone here have any experience with this? Thanks =)
Re: How to load another image randomly on click in php?
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="" />';Re: How to load another image randomly on click in php?
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.
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.
Re: How to load another image randomly on click in php?
Link the image to your script.
Code: Select all
echo '<a href=""><img src="' . $imageFolder . '/' . $imageFileName . '" alt="" /></a>';Re: How to load another image randomly on click in php?
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:
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.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
Re: How to load another image randomly on click in php?
Code: Select all
echo '<a href="SCRIPTNAME.PHP"><img src="' . $imageFolder . '/' . $imageFileName . '" alt="" /></a>'Re: How to load another image randomly on click in php?
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:
In what folder should I save the page-with-script.php file? I'm new to php, any help is grealty appreciated!
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>'
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.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
Re: How to load another image randomly on click in php?
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?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="" />';
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?