Jurik Posted January 5, 2007 Share Posted January 5, 2007 Hi guys. I have a folder with a load of images in, all of which are named diffrently. They are all .jpg however so that makes things a little bit easier I was just wondering how I would go about setting up a peice of php code that will pick one of these images and display it. Basicaly a random image generator I have googled it but all the tutorials seem to only work if you name the images 1.jpg 2.jpg etc any help would be great. Link to comment https://forums.phpfreaks.com/topic/32954-how-to-create-a-radom-image-displayer/ Share on other sites More sharing options...
taith Posted January 5, 2007 Share Posted January 5, 2007 $pictures=glob('images/*.jpg');echo $pictures[rand(0,count($pictures)-1)]; Link to comment https://forums.phpfreaks.com/topic/32954-how-to-create-a-radom-image-displayer/#findComment-153455 Share on other sites More sharing options...
kenrbnsn Posted January 5, 2007 Share Posted January 5, 2007 Another way:[code]<?php$pictures = glob('images/*.jpg');echo '<img src="' . $pictures[array_rand($pictures)] . '">';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/32954-how-to-create-a-radom-image-displayer/#findComment-153459 Share on other sites More sharing options...
Jurik Posted January 5, 2007 Author Share Posted January 5, 2007 ok I have tried both methods and for the first one it prints out '; ?> while the second example just displays blank, any ideas? Heres what ive done<?php $pictures=glob('PrizeGiving/thumbnails/*.jpg');echo $pictures[rand(0,count($pictures)-1)];?> Link to comment https://forums.phpfreaks.com/topic/32954-how-to-create-a-radom-image-displayer/#findComment-153494 Share on other sites More sharing options...
taith Posted January 5, 2007 Share Posted January 5, 2007 try print_r($pictures); is the array got anything in it? Link to comment https://forums.phpfreaks.com/topic/32954-how-to-create-a-radom-image-displayer/#findComment-153495 Share on other sites More sharing options...
obsidian Posted January 5, 2007 Share Posted January 5, 2007 First, you're declaring a relative path from the current file, so make sure your script is sitting in the folder containing the [i]PrizeGiving[/i] directory. Once you've done that, try printing out your glob() results and see if it's actually picked up your image names:[code]<?php// Make sure that your images are named appropriately and that you have// your case correct for your full path$pictures = glob("PrizeGiving/thumbnails/*.jpg");print_r($pictures);?>[/code]If the above code, does indeed collect all your image names, here's another alternative to get a random image:[code]<?php$pictures = glob("PrizeGiving/thumbnails/*.jpg");shuffle($pictures);echo "<img src=\"{$pictures[0]}\" />";?>[/code]Good luck! Link to comment https://forums.phpfreaks.com/topic/32954-how-to-create-a-radom-image-displayer/#findComment-153496 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.