Image/Advert rotator, need a little help

Discussion in 'PHP' started by martyn11post, Jul 19, 2008.

  1. #1
    Hi, I've been using a simple method to rotate my adverts on my site, but want to upgrade it so I can display more than one advert without the risk of showing the same advert/image twice.

    Here's what I have been using:
    <?php  $rnd = rand(1, 10);
    $filename = "ads" . $rnd;
       
    
        include ("basic/$filename.php");
         ?>
    PHP:
    Where a file called ads(No.).php would contain the html/javascript etc.
    But if I repeated the code twice I would run the risk of showing the same advert again. Ideally I want to show 3 and would rather not use MySql.

    Cheers,
    Martyn
     
    martyn11post, Jul 19, 2008 IP
  2. rob_v

    rob_v Peon

    Messages:
    72
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could try this
    <?php
    $rnd = rand(1, 10);
    $track_array[]=$rnd;
    $counter=0;

    while ($counter <=3)
    {
    if (!in_array($rnd,$track_array)
    {
    include ("basic/$filename.php");
    $counter++;
    $track_array[]=$rnd;
    }
    $rnd = rand(1, 10);
    }
    ?>
     
    rob_v, Jul 19, 2008 IP
  3. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Use sessions to track which ads have been served to each client.
    Rob, i think he is talking about seperate page loads, so the counter wouldn't work.
     
    matthewrobertbell, Jul 19, 2008 IP
  4. martyn11post

    martyn11post Guest

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What Rob put is what I'm after, one problem, it doesn't want to work. Unexpected "{" comes up?
     
    martyn11post, Jul 19, 2008 IP
  5. rob_v

    rob_v Peon

    Messages:
    72
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry missing a closing ) in the if

    make it this :
    if (!in_array($rnd,$track_array))



    basically it will allow 3 unique files on 1 page load.

    but like matthew said separate page loads will require sessions.
     
    rob_v, Jul 19, 2008 IP
  6. AdSpeed

    AdSpeed Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Another simplistic approach is to use microtime() and get the modulus (a % b) with the number of ads.
     
    AdSpeed, Jul 19, 2008 IP
  7. martyn11post

    martyn11post Guest

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks Rob that solves the problem and looks good. I think I will leave separate page loads to the future.
    Cheers,
    Martyn
     
    martyn11post, Jul 20, 2008 IP