Help: simple 50/50 rotation script

Discussion in 'PHP' started by mudanoman, Sep 18, 2006.

  1. #1
    Hello,

    I would like to implement a 50/50 rotator that will switch between either a default value or one from a variable.

    So something like

    function getvalueid()
    {
    rand (50% of time) = x
    if x > 50
    return default valueid
    if X < 50
    return variable valueid
    }

    I assume I can call it by doing something like <? echo getvalueid() ?> ?? something like that?

    Any help appreciated :)

    Thanks,

    Ivan
     
    mudanoman, Sep 18, 2006 IP
  2. Wekky

    Wekky Active Member

    Messages:
    192
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #2
    
    <?
    function switch(){
    $tmp=rand(1,100);
    $yourvalue=999;
    $myvalue=0;
    if ($tmp<50){
    return $yourvalue;
    } else {
    return $myvalue;
    }
    }
    ?>
    
    Code (markup):
    not tested.

    call with:
    
    echo switch();
    
    Code (markup):
     
    Wekky, Sep 18, 2006 IP
  3. mudanoman

    mudanoman Guest

    Messages:
    596
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #3
    awesome, thanks I will report back. -Ivan :)
     
    mudanoman, Sep 19, 2006 IP
  4. ThomasNederman

    ThomasNederman Peon

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I would say if you are thinking about using the script with adsense and have several ads on one page. You need to have the same pub ID on all ads that are displayed on one page. You can not have different PUB_ID on the same page. Just a reminder !

    What you need to do (for several displays this is)

    $PUB_ID = switch();

    goole_PUB = <? echo $PUB_ID;?>

    Etc to make sure you have the same on all ads
     
    ThomasNederman, Sep 19, 2006 IP
  5. Zeras

    Zeras Guest

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you only need a 50/50 random:

    function flipcoin($value1="",$value2="") {
       if (rand(0,1)) { return($value1); } else { return($value2); }
    }
    Code (markup):
    Then, in your program:

    <?= flipcoin("firstvalue","secondvalue"); ?>
    Code (markup):
    -Zeras
     
    Zeras, Sep 19, 2006 IP
  6. mudanoman

    mudanoman Guest

    Messages:
    596
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for everyone's help. I am still learning PHP, so excuse my newbiness :p

    Anyway...here is what I am trying to do...I dont think I have it setup right and the logic is quite odd?! Trying to piece it together based on what the programmer left off doing.

    
    
    // Sets default to Admins ID or if there is a user ID then sets that into $publisher_id variable
    
    if(!ted123_db_num_rows($sql_test_query)){
    	$publisher_id = ADMIN_PUBLISHER_ID;
    }else{
    	$sql_test = guru21_db_fetch_array($sql_test_query);
    	$publisher_id = $sql_test['ad_publisher_id'];
    }
    
    // This is probably a bit repetitive but essentially, after getting the publisher_id (either users or admins), it will randomly rotate it
    
    function flipcoin($value1="",$value2="") {
       if (rand(0,1)) { return($value1); } else { return($value2); }
    }
    
    // calls rotate function which returns the publiser id to display
     
    $publisher_id = flipcoin("$publisher_id","ADMIN_PUBLISHER_ID"); 
    Code (markup):

    Thanks for everyones help thus far :)
     
    mudanoman, Sep 21, 2006 IP