Jump to content

How to create a radom image displayer.


Jurik

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.