I had a site on which I wanted to include random thumbnails linking to galleries from a list of possible choices, without any duplicates showing up.

So, I turned to PHP which made this task insanely easy (of course!).

Anyway, I wanted to share how I did it in hopes that it'll be of use to some of you that are new to PHP.

Note: This method can be used anytime you want random rotation of text, HTML, or images.

Brief Explaination: I created an array containing the HTML for all the images I wanted to show up. I applied the shuffle function to that array to randomize the order, and I inserted the HTML into my page by just echoing the various values in the array where I wanted them.

Example:

First, I create the array with the HTML values I want to randomly show up.

$faketgp[0] = "<a href=\"URL TO GALLERY\" target=\"_blank\"><img src=\"PATH TO IMAGE\" border=\"0\" ></a>";
$faketgp[1] = "<a href=\"URL TO GALLERY\" target=\"_blank\"><img src=\"PATH TO IMAGE\" border=\"0\" ></a>";
$faketgp[2] = "<a href=\"URL TO GALLERY\" target=\"_blank\"><img src=\"PATH TO IMAGE\" border=\"0\" ></a>";

After that, I simply use PHP's handy dandy shuffle function to mix the values up!

shuffle($faketgp);

That was it! Now, I just insert additional code in my page to note where I want the images to show up.

<? echo $faketgp[0]; ?>
<? echo $faketgp[1]; ?>
<? echo $faketgp[2]; ?>

Each location in my page that has one of those lines will display one of the possible images and links. The order will be different each time the page is refreshed.

Note that you'll have to make sure the numbers are different, if there are duplicates they'll display the same image. You can always have more possible values than values you actually use in your page - this is actually better because having lots of possible values decreases the chance a visitor will see the same thing too often.

To me, this method of manually putting in each instance of an image is easier than some of the other ways I've seen the same sort of thing done.

One problem with this that comes to mind off hand, is that the images are displayed randomly EVERY time. That means some visitors might just keep hitting refresh to see different galleries.

Also, you can easily modify these basic pieces to make more complex and sophisticated display and rotation scripts.