How to create a random image order?

I have a gallery page and in the center of it I have a little table that shows some photos but I want the little table to do 2 things:

  1. show a photo and then show another (not scroll through them, but actually be showing one photo and then switch to another)
  2. to show the photos in a random order. I don’t always want the same photos to show everytime you load the page.

Can anyone help me???

Well, there are several ways to do this. One way would be to use a header that sleeps for a number of seconds and then refreshes the page. On the code where you display the image, you can use the PHP random function to select a picture. This would work, but, might be annoying to the user as it refreshes.
Or, you could create Javascript routine to do this also.

How often do you wish this to change the picture?

Also, there is a nice display at this link which you might like to try:
http://phphelptutorials.com/demos/galleryfromfolder/
(Just refresh the page a few times…)
You can find out how to do this at that website, just go to the base address.

Its more that I am creating a photography website and for the galleries pages I want their to be a sort of slideshow. Right now I have them sliding in and out of a table but in a specific order. Is there anyway to have them in a random order?

Yes, lots of ways. Did you look at the site I sent to you? How are you currently displaying them?

If you are pulling them from a directory listing of a folder, then, you could just skip a random number of pictures. Usually, if they are inside one folder, you could create an array of the filenames and use PHP random function to select one randomly. If you want to make sure that all of the pictures are shown, you would have to create a second array that is marked as they are shown. (If the random number of the picture is selected, you would have to check to see if it was shown already.) This can be an issue as you show more and more pictures. Lots of ways to do this. But, without more info on how you are doing it now, we can not help. So, to help you, we need to know where these pictures are. In a database? In a folder? Do you have them numbered in some way or just filenames? You already are showing them as you said, sliding them into place. But, we don’t know how you are doing this. So, help us help you…

Ok, so what I am trying to do is actually create a photo slideshow in the center of the page and I want the slideshow to be in a random order.

Right now I have been using IMAGE

I am still a learner at PHP. I am trying to build this project to learn the language etc. Does that make any sense?

The photos by the way are in a folder, my www folder. I have tried to name them with numbers according to their specific gallery ie a1, a2, a3 for animals etc.

Well, using “IMAGE”, you can enter many images between the tags.
Therefore, you could create a PHP script that would take the folder, pull so many picture names
randomly from the folder list of filenames and then create an IMAGE list to put inside the marquee tags.
So, first, you need to learn about accessing folder lists. This is code that will read a directory file list and store the filenames into an array:
[php]
$filenames = array();
$handle = opendir(’/YOURpath/to/files’);
// Loop thru the directory and load into array…
while (false !== ($filename = readdir($handle)))
$filenames[] = $filename;
closedir($handle);
[/php]
I did not test the above, but it should work. Next, you would have to randomly pick a few of these filenames to create image tags to put between your marquee tags. Something like this:
[php]
$i=count($filenames); // Get number of pictures…
$randnum = mt_rand(0, $i); // Pick one…
echo “”;
[/php]
I did not test this either, but should work. You will have to test it and debug it for yourself.
To debug the file part, use Print_r($filenames); to see if it captured the file list okay… Etc…

Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service