Making a PHP rotator page with priority in percentage

Discussion in 'PHP' started by johnb123, Apr 23, 2011.

  1. #1
    <?php

    $urls[] = "http://www.google.com";
    $urls[] = "http://www.ebay.com";

    srand ((double) microtime( )*1000000);
    $random = rand(0,count($urls));

    header("location:" . $urls[$random]);
    exit;
    ?>


    It's a random rotation.

    I need to add a percentage priority to the url.

    ie: 80% to url1, 20% to url2.
    This will make the url1 to rotate 80% of the time...

    if you have a similar script or can help about what I need, feel free to post :)

    thx.
     
    johnb123, Apr 23, 2011 IP
  2. Sepehr

    Sepehr Peon

    Messages:
    568
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    just add array elements with the element you want!

    so if you have an array with 5 values from which 4 are google.com then the chance of it showing up is 80% compared to the 20% of ebay.com.

    This is a simple and effective way for implementing this but if you have too many URLs and want to have more precised percentages(i.e 73% and 27%) you might want to try a more complex algorithm.
     
    Sepehr, Apr 24, 2011 IP
  3. johnb123

    johnb123 Active Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #3
    Yeah but I manage to have way more url and I need precise % like you said :\
     
    johnb123, Apr 24, 2011 IP
  4. blakeembrey

    blakeembrey Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm not sure if this is what your looking for, but I just quickly made it and tested it briefly and it seems to work fairly well.

    <?php
    $url = array("20" => "http://google.com/", "40" => "http://forums.digitalpoint.com/", "100" => "http://blakeembrey.com/");

    $random = rand(1, 100);

    $closest = 100;
    foreach($url as $k => $u) {
    if($k <= $closest && $random <= $k) {
    $show = $u;
    $closest = $k;
    }
    }

    echo $show;
    ?>

    This way you just set the value before the url to the percentage you want. The only problem here is that you have to actually calculate it yourself when you type it in. It probably isn't the ideal solution, but it's all I can think of at the moment that will actually work to the exact perentage.

    E.g. The solution above will have a 20% chance of drawing google.com, a 20% chance of drawing the forums and a 60% chance of drawing my website.
     
    blakeembrey, Apr 24, 2011 IP
  5. johnb123

    johnb123 Active Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #5
    nice, we progress! :)
     
    johnb123, Apr 25, 2011 IP
  6. blakeembrey

    blakeembrey Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yeah :) Sorry I dont have a more elegant solution, but I can't think of one at the moment. I'll keep thinking ;)
     
    blakeembrey, Apr 25, 2011 IP