Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Show 1 of 4 html divs based on ratio?

Options
  • 07-07-2008 4:31pm
    #1
    Registered Users Posts: 3,146 ✭✭✭


    I haven't been able to word this request succinctly enough for Google or Krugle :-/

    Essentially I have 4 divs. Each is assigned a ratio, for example

    show div1 1% of the time, div2 9%, div3 30% and div4 60%

    Each page load is a fresh start so a random number is being used.

    All I really need is the concept (how to calculate and select), although I'm using javascript to do a show/hide.

    Anyone got an idea, please?

    It is what it's.



Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    If each page load is a fresh restart you won't be able to do it as far as I can see...you need some sort of counter to calculate...I think...e.g. a related database.

    You also need to consider (I'm assuming it's advertising or something similar) that if Mr.X and Mr.Y are both hitting refresh repeatedly, are they both to receive the ratios equally, or is it just a case that overall they should be shown x amount of times regardless of the viewer?


  • Registered Users Posts: 3,146 ✭✭✭oneweb


    Thanks for the quick reply. Even though it won't be accurate I think it should still be possible to have a higher probability of a given div (eg div4) being selected?

    It is what it's.



  • Closed Accounts Posts: 81 ✭✭dzy


    Maybe something like this would work. Select a random integer between 1 and 100 and see what partition it falls into. The partitions are {1}, {2-10}, {11-40}, {41-100}.
    <?php
    
    $r = rand(1, 100);
    
    echo "<div id=\"";
    
    if ($r <= 1)
    {
        echo "1";
    }
    else if ($r <= 10)
    {
        echo "2";
    }
    else if ($r <= 40)
    {
        echo "3";
    }
    else
    {
        echo "4";
    }
    
    echo "\"/>"
    
    ?>
    


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Well yes, you could load a random number between 1 and 100 upon page load, and then in your select code you can use some code to the effect of
    if ($random_number is equal to 1) {
    
    print div 1
    }
    if ($random_number is between 2 and 10 ) {
    
    print div 2
    }
    if ($random_number is between 11 and 40) {
    
    print div 3
    }
    if ($random_number is between 41 and 100 ) {
    
    print div 4
    }
    

    It's rough...very rough... but it's about as good as it will get with no tracking method at all!


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    dzy, just beat me to it, but the code is flawed...what if 14 comes up? Divs 2 and 3 will be returned :)


  • Advertisement
  • Closed Accounts Posts: 81 ✭✭dzy


    Mirror wrote: »
    dzy, just beat me to it, but the code is flawed...what if 14 comes up? Divs 2 and 3 will be returned :)

    Are you sure? 14 isn't less than or equal to 10.


  • Registered Users Posts: 3,146 ✭✭✭oneweb


    Another point is that the divs aren't necessarily ordered from high to low probability but mixed, which could mean that two array iterations are needed and I'm hoping to avoid that.

    It is what it's.



  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    lol, never mind, I had meant to write 4, not 14, but then since you're using else...if rather than just if it doesn't matter :)


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    The order you set out is irrelevant, just use the code provided to simply determine which div is to be displayed, you don't need to echo the div right there.


Advertisement