Page 1 of 1
How to load another image randomly on click in php?
Posted: Wed Jul 20, 2011 11:23 pm
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 =)
Re: How to load another image randomly on click in php?
Posted: Thu Jul 21, 2011 10:11 am
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="" />';
Re: How to load another image randomly on click in php?
Posted: Fri Jul 22, 2011 4:02 am
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.
Re: How to load another image randomly on click in php?
Posted: Fri Jul 22, 2011 4:08 am
by Corvin
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?
Posted: Fri Jul 22, 2011 5:13 am
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>';
Re: How to load another image randomly on click in php?
Posted: Fri Jul 22, 2011 5:17 am
by Corvin
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?
Posted: Fri Jul 22, 2011 1:44 pm
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!
Re: How to load another image randomly on click in php?
Posted: Wed Jul 27, 2011 2:02 am
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?