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.

Rotating two bits of code

Discussion in 'Programming' started by blueuniverse, Dec 10, 2004.

  1. #1
    In the corner of my page I have an ad space, but I want to rotate this between the code from Trade Doubler and the code from Commission Junction. Both are javascript codes (you'll know what they look like if you use them, basically they're just jscript pools).

    Anyway, the page is vbulletin so PHP enabled (hence why I didn't put it in either the php or javascript forum, because I could use both to rotate the code).

    I only have 2 bits of code I need to rotate, but it is code not images.

    Does anybody know how I could achieve this?
     
    blueuniverse, Dec 10, 2004 IP
  2. disgust

    disgust Guest

    Messages:
    2,417
    Likes Received:
    133
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?
    
    $rand = rand(1,2);
    
    if ($rand == 1) {
      echo this code
    }
    elseif ($rand == 2 {
      echo this code
    }
    
    ?>
    Code (markup):
    you can change the ",2" bit in the rand statement to have more than two bits of code, and then just add more elseif's too if you like.
     
    disgust, Dec 10, 2004 IP
  3. blueuniverse

    blueuniverse Guest

    Messages:
    217
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ahhh, I've just realised that it is actually going to have to be javascript, beacause the header template file in vB won't accept php code :S
     
    blueuniverse, Dec 10, 2004 IP
  4. Gandalph

    Gandalph Active Member

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    58
    #4
    ahh, just a tiny bit of refinement, so you can post your adsense code and other codes in here without php proofing them ;)

    <?php
    // change the ",2" bit in the rand statement to have more than two bits of code, and add more elseif's if you like
    $rand = rand(1,2);
    
    if ($rand == 1){
    ?>
    
    place first html/javascript ad code here
    
    <?php
    } elseif($rand == 2){
    ?>
    
    place 2nd html/javascript ad code here
    
    <?php
    }
    ?>
    
    PHP:
     
    Gandalph, Oct 17, 2005 IP
  5. maro

    maro Peon

    Messages:
    356
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you want it entirely in javascript:

    random_num = Math.round(Math.random());
    if (random_num == 0)
    {
    // What you want to do 1
    }
    else
    {
    // What you want to do 2
    }
     
    maro, Oct 18, 2005 IP
  6. creepah

    creepah Peon

    Messages:
    313
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks for that bit of code very helpful :)
     
    creepah, Dec 22, 2005 IP
  7. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #7
    If there are only two, make it boolean.

    
    if( rand(0,1) )
    {
        // $display_var = evaled code for 1
    }
    else
    {
         // $display_var = evaled code for 0
    }
    
    PHP:
    vBulletin 3.5.x hook global_start
    vBulletin 3.0.x php_include_start template
     
    noppid, Dec 22, 2005 IP