Several random images on 1 page

Discussion in 'JavaScript' started by willemr, Oct 28, 2010.

  1. #1
    Hey all,

    My plan is to make a webpage full of images of 10x10 pixels next to each other.
    These images should randomly be either WHITE (2 of the 3 images in the "random" map), or BLACK (1 image).
    I want those images right next to and right under each other (so IFRAMES and the like is not a solution) - the goal is to generate a random pattern of black 10x10 pixel images....

    I suppose this should be rather easy to script for those who are no JS-virgins like me, however, i can't find any script on the net that does the random image trick for MORE THAN 1 IMAGE ON 1 PAGE.

    I hope you guys can help me out! I'm getting kind of desperate.

    Thanks !
    W
     
    willemr, Oct 28, 2010 IP
  2. vitalbusiness

    vitalbusiness Peon

    Messages:
    882
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can try a different approach. Just put this simple php image rotator code next to each other in one row, then divide it by a <br> and then put it next to each other in the second row, etc. Each code will be pulling random images out.

    First save your images in an images folder.

    ------------------------------

    Then save this as a text file: banner_ads.txt


    images/your_image_1.gif
    ~
    images/your_image_2.gif
    ~
    images/your_image_3.gif
    ~

    Notice: The ~ has to be there exactly the way it is shown. This is what separates images for the php code. Add as many images as you want. It should end with a ~

    Then place this code next to each other:

    <?php

    $fcontents = join ('', file ('banner_ads.txt'));
    $s_con = split("~",$fcontents);

    $banner_no = rand(0,(count($s_con)-1));
    echo $s_con[$banner_no];
    ?>

    It should do the trick. No javascript will be needed. Keep in mind your pages have to be saved in .php for this to work.
     
    vitalbusiness, Oct 28, 2010 IP
  3. vitalbusiness

    vitalbusiness Peon

    Messages:
    882
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Your rows will look like this:


    <?php

    $fcontents = join ('', file ('banner_ads.txt'));
    $s_con = split("~",$fcontents);

    $banner_no = rand(0,(count($s_con)-1));
    echo $s_con[$banner_no];
    ?>
    <?php

    $fcontents = join ('', file ('banner_ads.txt'));
    $s_con = split("~",$fcontents);

    $banner_no = rand(0,(count($s_con)-1));
    echo $s_con[$banner_no];
    ?>
    <?php

    $fcontents = join ('', file ('banner_ads.txt'));
    $s_con = split("~",$fcontents);

    $banner_no = rand(0,(count($s_con)-1));
    echo $s_con[$banner_no];
    ?>
    <?php

    $fcontents = join ('', file ('banner_ads.txt'));
    $s_con = split("~",$fcontents);

    $banner_no = rand(0,(count($s_con)-1));
    echo $s_con[$banner_no];
    ?>

    <br>

    <?php

    $fcontents = join ('', file ('banner_ads.txt'));
    $s_con = split("~",$fcontents);

    $banner_no = rand(0,(count($s_con)-1));
    echo $s_con[$banner_no];
    ?>
    <?php

    $fcontents = join ('', file ('banner_ads.txt'));
    $s_con = split("~",$fcontents);

    $banner_no = rand(0,(count($s_con)-1));
    echo $s_con[$banner_no];
    ?>
    <?php

    $fcontents = join ('', file ('banner_ads.txt'));
    $s_con = split("~",$fcontents);

    $banner_no = rand(0,(count($s_con)-1));
    echo $s_con[$banner_no];
    ?>
    <?php

    $fcontents = join ('', file ('banner_ads.txt'));
    $s_con = split("~",$fcontents);

    $banner_no = rand(0,(count($s_con)-1));
    echo $s_con[$banner_no];
    ?>
     
    vitalbusiness, Oct 28, 2010 IP
  4. willemr

    willemr Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the reply!!
    I tried this out, but didn't work out fine (no doubt due to my very, very limited knowledge of PHP).
    Is there no way to do this in Javascript (as I don't know anything about PHP, i just have a (really) basic knowledge of HTML & JS)??

    Thanks again -
     
    willemr, Oct 29, 2010 IP