How to automaticlly cycle ads on my homepage

Discussion in 'Web Design and Development' started by macaddict23, May 4, 2007.

  1. macaddict23 macrumors 6502

    macaddict23

    Joined:
    Jun 20, 2006
    Location:
    MacVille, USA
    #1
    I have four different ads that I would like shown on the left-hand side of my homepage. I want a different ad to show up each time my homepage refreshes, or each time a user visits the page. The ad is basically a 72x72px image with about 8 lines of HTML text wrapped around it. How can I make this happen? Thanks in advance!
     
  2. yagran macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #2
    do you want it random or cyclic? do you need a ad show count?
     
  3. macaddict23 thread starter macrumors 6502

    macaddict23

    Joined:
    Jun 20, 2006
    Location:
    MacVille, USA
    #3
    I want one of the ads to show up four times as often as the other three. No, I don't need a ad show count. Thanks! :)
     
  4. yagran macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #4
    ok so put the following code in you page where you would like the banners to show.

    Code:
    <Script src="banners.js" type="text/javascript" language="javascript"></script>
    next create an html page for each of your banner ads with the picture and text layed out as you want them.

    then download the attached javascipt file and open it up, use the comments to help you configure it.

    then upload your the lot... should work :)

    (adapted from http://www.dynamicdrive.com/dynamicindex17/randomiframe.htm)
     

    Attached Files:

  5. macaddict23 thread starter macrumors 6502

    macaddict23

    Joined:
    Jun 20, 2006
    Location:
    MacVille, USA
  6. macaddict23 thread starter macrumors 6502

    macaddict23

    Joined:
    Jun 20, 2006
    Location:
    MacVille, USA
  7. nicksoper macrumors member

    nicksoper

    Joined:
    Mar 6, 2006
    Location:
    Cape Town
    #7
    php?

    I'd probably use some php to generate a number on load and then use that variable in the path of a php include file, which would have your banner code inside.

    I'm not a huge php buff, but you could do something like this:
    Put this above the doctype.
    Code:
    <?php
    $includes_folder = "../includes";
    $include_name = not really sure of that part, check out a php site
    ?>
    
    then where you put your banner, use an include thing.
    Code:
    <?php include("$includes_folder/$include_name"); ?>
    
     
  8. yagran macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #8
    erm it should do..
    hmm...
     
  9. virus1 macrumors 65816

    virus1

    Joined:
    Jun 24, 2004
    Location:
    LOST
    #9
    Here's the question: Does it need to rotate while the site is open, or just on every load? (Hint: it is much easier to do it on every page load)
     
  10. macaddict23 thread starter macrumors 6502

    macaddict23

    Joined:
    Jun 20, 2006
    Location:
    MacVille, USA
    #10
    No, it does not have to rotate while the site is open. So yes, I want a new ad to rotate on every page load. Thanks!
     
  11. virus1 macrumors 65816

    virus1

    Joined:
    Jun 24, 2004
    Location:
    LOST
    #11
    OK, so assuming you have php turned on, simply put this code in your site where you want the banner to show up.
    Code:
    <?php
    $therandom = rand(1, 7);
    if ($therandom < 5){
    echo "<img src=\"BannerThatShowsUp4xAsOftenURL\">";
    } else if ($therandom == 5){
    echo "<img src=\"SecondBannerURL\">";
    } else if ($therandom == 6){
    echo "<img src=\"ThirdBannerURL\">";
    } else if ($therandom == 7){
    echo "<img src=\"FourthBannerURL\">";
    } else {
    echo "the virus messed up!";
    }
    ?>
    
    this is untested so tell me if you have any troubles
     
  12. yagran macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #12
    word on the street is that mt_rand() is a much faster more efficient algorithm so id opt for this slightly diffent way:
    Code:
    <?php
    $therandom = mt_rand(1, 7);
    if ($therandom < 5){
    echo "<img src=\"BannerThatShowsUp4xAsOftenURL\">";
    } else if ($therandom == 5){
    echo "<img src=\"SecondBannerURL\">";
    } else if ($therandom == 6){
    echo "<img src=\"ThirdBannerURL\">";
    } else if ($therandom == 7){
    echo "<img src=\"FourthBannerURL\">";
    } else {
    echo "error in random function";
    }
    ?>
    
    nice work tho :D
     
  13. virus1 macrumors 65816

    virus1

    Joined:
    Jun 24, 2004
    Location:
    LOST
    #13
    Thanks. I'll use that next time.

    I myself am relatively new to php. In fact, that was my first time using any random function at all.
     
  14. yagran macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #14
    ye im learning php javasript actionscript and c++ all at once. :p haha
     
  15. Moria macrumors regular

    Joined:
    May 7, 2005
    Location:
    Glasgow, Scotland
    #15
    Try out a PHP script called PHPmyads (or similar, look on www.hotscripts.com). It's really powerful, gives you full details about all your ads and lets you choose how often they show up.
     
  16. weg macrumors 6502a

    weg

    Joined:
    Mar 29, 2004
    Location:
    nj
    #16
    You don't need PHP for that, JavaScript is sufficient:

    <html>
    <head>
    <script language = "JavaScript">
    <!--
    function choose () {
    if (document.images) {
    var z = Math.floor(Math.random()*2);
    switch (z)
    {
    case 0:
    document["myimg"].src = "pics/pic0.jpg";
    break;
    case 1:
    document["myimg"].src = "pics/pic1.jpg";
    break;
    }
    }
    }
    -->
    </script>
    <body onload="choose()">
    <img name="myimg" src="pics/nopic.jpg">
    </body>
    </html>

    pics/pic0.jpg and pics/pic1.jpg are your ads, and pics/nopic.jpg can be a placeholder (e.g., a white pixel picture). When the page is loaded, the placeholder is replaced by a random picture (adjust the Math.random()*2 and add new switch clauses if you want more than 2 pictures). Sorry for the lack of indentation, I was too lazy.
     
  17. virus1 macrumors 65816

    virus1

    Joined:
    Jun 24, 2004
    Location:
    LOST
    #17
    It would work that way, but:
    -PHP is more simple
    -PHP is non client based
    -Harder to define probabilities with your script
     
  18. yagran macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #18
    aye. i agree with you. use the php version
     
  19. weg macrumors 6502a

    weg

    Joined:
    Mar 29, 2004
    Location:
    nj
    #19
    Well, for a newbie who hasn't configured Apache (or whatever) to serve PHP pages, probably not..
     
  20. yagran macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #20
    but surely a 'newbie' wouldnt be running his own server but would more likely have rented hosting so it would already be set up?
     

Share This Page