treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Ad random rotate

  • Can anyone help me out please.

    Ive done something like these http://css-tricks.com/random-ad-positions/ examples on my site but i don't know how to make the images into links ?

    As you can tell i don't know much about PHP :o

    Thanks
  • Depending on the link of course...

    <?php
    $totalImages = 6;

    $randomFirst = rand(1,$totalImages);

    for ( $i=$randomFirst; $i <= $totalImages; $i++ ) {
    echo \"<a href=\"link\"><img src='images/ad-$i.png' alt='ad' /></a> \";
    }

    for ( $i=1; $i < $randomFirst; $i++ ) {
    echo \"<a href=\"link\"><img src='images/ad-$i.png' alt='ad' /></a> \";
    }
    ?>
  • "Robskiwarrior" said:
    Depending on the link of course...

    <?php
    $totalImages = 6;

    $randomFirst = rand(1,$totalImages);

    for ( $i=$randomFirst; $i <= $totalImages; $i++ ) {
    echo \"<a href=\"link\"><img src='images/ad-$i.png' alt='ad' /></a> \";
    }

    for ( $i=1; $i < $randomFirst; $i++ ) {
    echo \"<a href=\"link\"><img src='images/ad-$i.png' alt='ad' /></a> \";
    }
    ?>



    Hi Rob....thanks for the quick reply....again :D

    Ive gone for the second method so this is my code

    <?php
    $totalImages = 3;

    $all = range(1,$totalImages);
    shuffle($all);

    foreach ($all as $single) {
    echo "<img src='http://link/ad-$single.jpg' alt='ad' /> ";
    }
    ?>

    So do you know how i would apply it to this method ?

    Thanks again
  • well you just need to wrap your echo'ed <img> tag in an anchor tag. Do you know where it is linking? is each link separate? Where are you getting the link from?
  • "Robskiwarrior" said:
    well you just need to wrap your echo'ed <img> tag in an anchor tag. Do you know where it is linking? is each link separate? Where are you getting the link from?



    Yeh each link will be separate. At the moment there is just 3 links/images, two will go to 2 different domains and one wil link to a page on my site.