1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

random img picker

Discussion in 'PHP' started by Silvester Vella, Jan 29, 2015.

  1. #1
    I found this simple code so that I can have a random image inserted into a div every time the page is loaded up.
    http://www.dyn-web.com/code/basics/random_image/random_img_php.php

    ..and it works great... now, how can I have it do the same thing to multiple divs?
    coz right now obviously it's loading the same image in every div.
    I'm totally new to php so please bear with me.
    Thanks.
     
    Silvester Vella, Jan 29, 2015 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Just define this once on your page:
    $imgList = getImagesFromDir($root . $path);
    PHP:
    and use this for image element anywhere?
    <img src="<?php echo $path . getRandomFromArray($imgList); ?>" alt="" />
    PHP:
     
    hdewantara, Jan 29, 2015 IP
  3. Silvester Vella

    Silvester Vella Greenhorn

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Thanks, that fixed that problem. Thing is that sometimes there still will be duplicates. I wold like to have the 4 divs all load different images.
    Any code for that?
    Thanks for your time.
     
    Silvester Vella, Jan 30, 2015 IP
  4. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #4
    You're welcome :)

    I haven't tested this, but logically... you may want to shuffle() the array $imgList first. Then, array_pop() an array-element really out of the array ( this replaces the previous getRandomFromArray() ).

    Careful though that you need to limit number of image-elements to be less than available image-files.

    Hendra
     
    hdewantara, Jan 30, 2015 IP
  5. Silvester Vella

    Silvester Vella Greenhorn

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    I appreciate your time mate but thats way too technical for me. I tried to look up shuffle and pop a bit but I dont really know how and were to insert any of the code.
     
    Silvester Vella, Jan 30, 2015 IP
  6. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #6
    Ah,
    What's so dangerous about some little experimentation of your own?

    Like before, call this once:
    $imgList = getImagesFromDir($root . $path);
    shuffle($imgList);
    PHP:
    Then for image-elements use the following:
    <img src="<?php echo $path . array_pop($imgList); ?>" alt="" />
    PHP:
     
    hdewantara, Jan 30, 2015 IP
    freelanceDeveloper likes this.
  7. Silvester Vella

    Silvester Vella Greenhorn

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    I did try a few stuff but nothing worked. Anyway your code worked perfectly mate, really appreciate your help. thanks alot :)
     
    Silvester Vella, Jan 30, 2015 IP