Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    3 random image/links on same page... no repeats

    I need to create a series of three random thumbnail images on a page w/ every reload.

    But none of the 3 can be repeats of the other two images.

    Anyone know a code for this? I know how to do random images, but I don't know how to ensure that there are no repeats.

    Thank you
    William

  • #2
    New Coder
    Join Date
    Apr 2005
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Using PHP with mySQL is generally a better solution than using JavaScript for this type of thing.

    If you absolutely have to use JavaScript, you can run the random image/text chosen through a for loop to make sure it doesn't match the others. There are a lot of scripts out there premade for you, just do a quick Google search and you'll find something.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Javascript solution (no loops required):

    Code:
    var imgArray = ['img1.gif','img2.gif','img3.gif', ......'img100.gif'];
    function randOrd(){return (Math.round(Math.random())-0.5); }
    imgArray.sort(randOrd);
    var pic1 = imgArray[0]; // first random image
    var pic2 = imgArray[1]; // second random image
    var pic3 = imgArray[2]; // third random image
    Still better done on the server though if you can.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New Coder
    Join Date
    Apr 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanx

    Thanks alot, that'll work great...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •