php random image selection

Discussion in 'HTML' started by Tim W, Feb 16, 2012.

  1. Tim W

    Tim W Guest

    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
     
    Tim W, Feb 16, 2012
    #1
    1. Advertising

  2. On Thu, 16 Feb 2012 17:38:20 +0000, Tim W wrote:

    > 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
     
    Denis McMahon, Feb 16, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Niederberger
    Replies:
    2
    Views:
    16,548
    Christian Kaufhold
    Jan 7, 2005
  2. Andrew Crowe
    Replies:
    1
    Views:
    4,481
    Andrew Crowe
    Sep 13, 2004
  3. globalrev
    Replies:
    4
    Views:
    778
    Gabriel Genellina
    Apr 20, 2008
  4. Rajive Narain
    Replies:
    0
    Views:
    1,936
    Rajive Narain
    Sep 18, 2009
  5. VK
    Replies:
    15
    Views:
    1,188
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page