Forum Moderators: coopster

Message Too Old, No Replies

changing pictures on index-page when reloading

How can you automatically show different pictures when re-entering a site

         

FelixH

7:14 pm on Feb 7, 2004 (gmt 0)

10+ Year Member



Hi!

I am a beginner but quite curious anyway.

My problem: I have seen websites that show different pictures whenever you enter the site. By doing this, the sites look always fresh and attractive and I like the idea a lot. Now I like to have the same function on my site but I do not know how that works...

Is there anybody who can give a good "explanation for dummies" on how to do that?

Thank you very much!

/Felix

jatar_k

8:31 pm on Feb 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



This isn't limited to php, it could be done with javascript as well, or any other scripting language for that matter.

The logic behind it is

You want to pick a random number between 1 and the number of images you have to display. You can name the images with numbers.
ie
1.jpg
2.jpg
3.jpg
etc ...

You use the chosen language to get the random number and then write it into the html output in the image tag. This way each time the page is loaded a random image will appear.

A code example in PHP with 5 images available might be something like this. All in the image tag

<img src="/img/<?= rand(1,5)?>.jpg" width="100" height="50" border="0">

That will load one of the 5 images.

Note: That example would be for php 4.2.0 and above where the rand function no longer needs to be seeded.