djmike Posted September 3, 2006 Share Posted September 3, 2006 I am looking for a PHP script that will randomly display an image from a folder of around 20 images, although there will be more soon. The images names are all initials, e.g. TY. I could do a script to pick images if their names were all number going up from 1, but I'm not sure if this is possible with the above?Thanks for any suggestions Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 How about using the glob() function to generate an array of the images and then using rand() to select an image from the array? Quote Link to comment Share on other sites More sharing options...
djmike Posted September 3, 2006 Author Share Posted September 3, 2006 Ok, could you give example code please. I understand the rand function, and have already got a function using the time() to get a random number. I just don't understand who I'd use glob() with the images to create an array?Thanks Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 [code]<?php// in the folder with the imagesforeach (glob("*.jpg") as $filename) { $image[$i] = $filename; $i++;}$rand_image = rand(0,count($image)-1);$tmp = $image[$rand_image];$size = getimagesize($tmp);echo "<img src='". $tmp. "' ". $size[3]. " alt='". $tmp. "'>";?> [/code] Quote Link to comment Share on other sites More sharing options...
djmike Posted September 3, 2006 Author Share Posted September 3, 2006 Thanks, that works fantastically.I just need to now change it so that upon clicking the picture (using <a href) the user is directed to a php page with ?t= followed by the initial that were the picture's name. So for the image TY.jpg, the link would be mysite.com/viewer.php?t=TYHowever, as I created an array, I only have the complete paths stored not the original files. Is the best way to create another array for the file names, ut I'm not sure how this would work it may randomise twice so the link doesn't match the imageThanks again Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 [code]<?php// in the folder with the imagesforeach (glob("*.jpg") as $filename) { $image[$i] = $filename; $i++;}$rand_image = rand(0,count($image)-1);$tmp = $image[$rand_image];$size = getimagesize($tmp);$url_path = "../relative/path/to/folder/where"; // where viewer.php exists - adjust to suit.// what's before the . in the image name?$bits = explode(".",$tmp); // $bits[0] will be TY, etc.$url = $url_path. "/viewer.php?t=". $bits[0];echo "<a href='". $url. "'><img src='". $tmp. "' ". $size[3]. " alt='". $tmp. "' border='0'/></a>";?>[/code]That should help you out as long as you have the right relative path from the images folder to viewer.php. You can always view-source on the generated image page to see what URL it's pointing to Quote Link to comment Share on other sites More sharing options...
djmike Posted September 3, 2006 Author Share Posted September 3, 2006 Thanks so much for the help, I got it sorted, but bits[0] display the whole file path and so didn't work. I had to explode it further to remove the file path:[code]$bits = explode(".",$tmp) ; // $bits[0] will be filepath/TY, etc.$bits2 = explode("path/",$bits[0]) ; // $bits2[1] will be TY, etc.[/code]Thanks again Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 3, 2006 Share Posted September 3, 2006 You're welcome. Glad it all worked out. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.