1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How do you rotate at x% within php?

Discussion in 'General Programming Chat' started by mightybh, Mar 31, 2012.

  1. mightybh

    mightybh Jr. VIP Jr. VIP Premium Member

    Joined:
    Feb 27, 2008
    Messages:
    1,029
    Likes Received:
    1,714
    Occupation:
    CEO
    Location:
    UK
    I have a bit of php code that includes this:

    $call_url = "http://www.blablabla.com?id=xxxxxxxx&show=2&subid=" . $user_subid . "&geoip=" . $user_ip;

    How do I rotate xxxxxxxx at a set %? For example I want it to show xxxxxxxxx 10% of the time, yyyyyyyyy 40% of the time and zzzzzzzzz 50% of the time.

    Any ideas? Thanks!
     
  2. 45415

    45415 Junior Member

    Joined:
    Feb 5, 2009
    Messages:
    194
    Likes Received:
    92
    PHP:
    if(rand(1,10) == 10// 10%
    PHP:
    if(rand(1,2) == 2// 50%
     
    • Thanks Thanks x 2
  3. extremephp

    extremephp BANNED BANNED

    Joined:
    Oct 19, 2010
    Messages:
    1,293
    Likes Received:
    1,272
    PHP:
    if(rand(1,10) == 10// 10%
    Are you sure the rand function will be 10 once every 10 tries? I thought it was completely random and rand function can output a 10 maybe every 5 tries, or may be after 15 times. Am just asking :)
     
  4. 45415

    45415 Junior Member

    Joined:
    Feb 5, 2009
    Messages:
    194
    Likes Received:
    92
    It will not be 10 exactly once per 10 tries, but in the long term it will be around 10%. I am using this to switch different ad units, and in the stats I can see that the views are evenly distributed, so each ad unit has about the same number of views. Of course it is not exactly 10.00% for each, it could be 10.5% or 9.5%. To get exactly 10%, you probably have to save the number of requests to the page and then output the code after each 10 requests. If I am wrong, somebody please correct me and let us know the right way of doing this.
     
    • Thanks Thanks x 1
  5. mightybh

    mightybh Jr. VIP Jr. VIP Premium Member

    Joined:
    Feb 27, 2008
    Messages:
    1,029
    Likes Received:
    1,714
    Occupation:
    CEO
    Location:
    UK
    So how would I apply this to my problem?

    Thank you!
     
  6. 45415

    45415 Junior Member

    Joined:
    Feb 5, 2009
    Messages:
    194
    Likes Received:
    92
    Something like this:
    PHP:
    $rand rand(1,10);
    if(
    $rand == 1) {
      
    $your_variable 'xxx'// 1
    }
    else if(
    $rand >= && $rand <= 5) {
      
    $your_variable 'yyy'// 2 3 4 5
    }
    else {
      
    $your_variable 'zzz'// 6 7 8 9 10
    }
    $call_url "http://www.blablabla.com?id=" $your_variable "&show=2&subid=" $user_subid "&geoip=" $user_ip;
     
    • Thanks Thanks x 2
  7. hameem

    hameem Elite Member

    Joined:
    Jul 21, 2011
    Messages:
    1,880
    Likes Received:
    985
    Location:
    221B Baker Street
    If I understood it correctly, you can use it like this:

    PHP:
    <?php
    if(rand(1,10) == 10) {
      
    $showMe xxxxxxxxxx
    }
    elseif(
    rand(1,10) == 15){
      
    $showMe yyyyyyyyyy;
    }

    $call_url http://blabla.com?id=$showMe&show=2&subid=....

    ?>
     
  8. extremephp

    extremephp BANNED BANNED

    Joined:
    Oct 19, 2010
    Messages:
    1,293
    Likes Received:
    1,272
    elseif(rand(1,10) == 15) :liar:

    454's code should work just fine. Still, I would like to see if this can be done with being strict on the percentages instead of depending on rand. Learning php lately, and these kinda questions seems so interesting.
     
  9. hameem

    hameem Elite Member

    Joined:
    Jul 21, 2011
    Messages:
    1,880
    Likes Received:
    985
    Location:
    221B Baker Street
    Yeah 45415's code is better and correct, ignore mine. Haven't touched PHP in a long time.
     
  10. hameem

    hameem Elite Member

    Joined:
    Jul 21, 2011
    Messages:
    1,880
    Likes Received:
    985
    Location:
    221B Baker Street
    @extremephp: to handle the percentage aspect strictly we'll probably have to use databse. Let's say we keep 3 variables, x, y and z in databse. Each time the page gets visited, x gets updated along with y and z. On each page visit the percentage of them showing gets calculated. Kinda like the following algorithm:

    Fetch x,y,z from db

    While x<101
    If x >0
    Then calculate % of y and z
    If y>10% then dont show y anymore
    Else show y's url and update the count of y
    If z>15% then dont show y anymore
    Else show z's url and update the count of z

    Reset x to 0
     
    Last edited: Mar 31, 2012