Need help on coding a script that selects and displays a random image?

Discussion in 'HTML & Website Design' started by HydroJohn, Feb 4, 2010.

  1. #1
    Hi guys.

    I'm a keen photographer and have hundreds of good photos.

    I want to build a very simple site that will just have my logo at the top a little bit of text then I want a picture.

    Here's the tricky part. I want to upload my hundreds of photos onto my server then I want a script that will select on of those pictures at random then display that image on the homepage.

    So each time the user refreshes it will pick a completely random picture again.

    Please could someone tell me the code for how to do this!

    Many Thanks :)
     
    HydroJohn, Feb 4, 2010 IP
  2. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Put all the images in a folder.
    Save this as randomImage.php and place in the same folder as images.
    On site, <img src="folder-of-images/randomImage.php" alt="" />

    This is probably the simplest way, if you don't want to name each image a number then use rand().'.jpg'; or whatever.
     
    krsix, Feb 4, 2010 IP
  3. mmiksik

    mmiksik Peon

    Messages:
    69
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi, I'm using jQuery,
    I do a div where I display images within as background images
    I give my images names ending with numbers from 0 to x-amount of pics
    and use something like this to generate random image within that div:
    (with a bit of fading in effect)
      $(document).ready(function(){
    var totalNum = 5; //the total number of images in a folder.
     var rndNum = Math.floor(Math.random() * totalNum);
     $("div#loader").hide(10)
     $("div#loader").css("background-image","url(_images/dental_implants" + rndNum  + ".jpg)");
     $("div#loader").fadeIn(1500);
      });
    
    Code (markup):
     
    mmiksik, Feb 4, 2010 IP
  4. instant87

    instant87 Active Member

    Messages:
    626
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    80
    #4
    instant87, Feb 4, 2010 IP