php random image selection

T

Tim W

You can't see it on my site at www.coleridgeway.com because I have just
been attempting it on wampserver but I have three different jpegs in a
row on the LHS and I am trying to implement a random image selector so
that each time a page is loaded you get three new and different images.
I have tried a couple of scripts for random images but they both
produced three times the same image each time the page was loaded.

Anyone know a script which will work?

Tim W
 
D

Denis McMahon

You can't see it on my site at www.coleridgeway.com because I have just
been attempting it on wampserver but I have three different jpegs in a
row on the LHS and I am trying to implement a random image selector so
that each time a page is loaded you get three new and different images.
I have tried a couple of scripts for random images but they both
produced three times the same image each time the page was loaded.

Anyone know a script which will work?

It's not that difficult to roll your own:

1) get the names of all the files in directory x that match any of *.gif,
*.jpg, *.png. Look up the php documentation for glob()

2) pick 3 at random. Look up the php documentation for array_rand()

3) display them in the html. Use echo.

Some string manipulation may be required. Glob() uses the filesystem
path. You might want to apply the basename() function to the three random
picks, and then prepend them with the path relevant to the webroot.

eg: url0 = "http://www.example.com/images/" . basename($files[$keys[0]]);

These are the bits that you need to glue together, have fun with your
construction kit toy. ;)

Rgds

Denis McMahon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top