By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

How to rotate code based on how many visits already recorded to DB?

P: 9
Hello all, and thanks again for the help, I have been very pleased with the quality posts on this forum!

Here what I need to solve:

I have 2 tables on a db, one is called visitor_log and here is the structure:

[PHP]
visitor_ID | visitor_IP | visitor_date (DATETIME FIELD) | article_ID
1 192.168.1.1 2008-09-02 01:12:19 10
2 192.168.1.2 2008-09-02 05:26:22 20
3 192.168.1.3 2008-09-02 06:45:54 30[/PHP]

And here is the other table:

[PHP]article_ID | article_content | click_through_percentage
1 (html content) 100
2 (html content) 80
3 (html content) 50[/PHP]

I have another table where I store code for 2 advertisements,one I called the main one and the other, the default one.Each page I display the article stored on the table above calls a query to the advertisement table and will always render the main advertisement code on the page. I can work this perfectly fine with PHP, but Im now required to be able to display the main ad code based on the click_through_percentage field value. So, if the php page that contains article_ID 2 is called, it needs to render the main ad 80% the time; the other 20% of the time it will render the default ad stored on the same table; if artilce_ID 3 is called, it needs to render the main ad 50% of the time, and other 50% the default ad, etc... so you got the drift. This percentage is based on the number of visits to the page, but it can also be like: if 50 visits are already accounted for this article, the other 50 should now show the default ad..etc.


I was suggested to use a random function to do all this, but since it is based on prediction, it probably wouldn't work I think.

Now since I'm inserting each visit to each article page on the visitor_log/article_ID field, How do I do this "math" with php based on the count of the article_ID field in the visitor_log table and achieve the result I need? Say, I now have a count of 50 visits to article_ID 3 on the visitor_log page, how would php be able to "know" that 50 visits have already been achieved to that article, so now it should render the default ad instead of the main one that it already shown/rendered 50 times? Also, after the default ad has also shown 50 times, it will now revert back to the main ad, and so on.

I hope I made sense! Thanks again!!!
Sep 8 '08 #1
Share this Question
Share on Google+
5 Replies


P: 8
You have very interesting assignments at your work ...

Here is my understanding of the situation:

you need to render either the main ad or a secondary add based on the click_through percentage.

On the long run and with thousans of cases, a random function will give you "roughly" the breakdown that you are looking for. But you are concerned with the randomness of the approach and would like to have some control in order to make sure that at all times you are pretty close of the desired breakdown say 80/20, or 60/40.

This is what I would do:

1. you need an element of randomness. You don't want to systematically display 8 times the main ad and after 2 times the secondary add. or 5 consecutive time the primary ad and 5 consecutive rimes the socndary ad (for a 50/50 breakdown).

2. you need an element of adjustement to the actual results.
to prevent exceeding the imposed breakdown.,

3. You need to keep track of the hits under each scenario. The detail table containing the hits would not be optimal (performance of count query every time the time you they visit the article). Perharps keeping track of the counter for ad usage on a per acticle basis is required

SO HOW DO WE DO IT?:

Expand|Select|Wrap|Line Numbers
  1.  
  2. // $click_through is the field in your articles table
  3. // $main_ad_cntr is your main ad counter from a table
  4. // $secondary_ad_cntr is your seconday add counter from a table
  5.  
  6. function which_add($click_trhough, $main_ad_cntr, $secondary_ad_cntr){
  7.  
  8.     $total_hits = $main_ad_cnt + $secondary_ad_cnt; 
  9.     $rand_nbr = generate random number for 0 to 100
  10.  
  11.    if $rand_nbr <=  than $click_through  {
  12.         if ( $total_hits == 0 )  { return 1; }  // random proposes main ad. First hit so breakdown is not off. primary ad OK
  13.  
  14.         if (  (  (main_add_cntr + 1) / $total_hits ) > ( $click_through / 100) ) 
  15.               { return 2; }  // random proposes main ad but breakdown is off , 
  16. choose secondary add
  17.          else {return 1;)
  18. }
  19. else
  20.          if ( $total_hits == 0 )  { return 2; } random proposes secondary ad. First hit so breakdown is not off. Seconday is OK
  21.        if (  (  (seconday_ad_cntr + 1) / $total_hits ) > ( 1 - ( $click_through / 100) )) 
  22.               { return 1; }  //  random proposes seconday ad but breakdown is off , choose primary ad
  23.             else {return 2;}
  24.  
  25. }
  26.  
  27.  
  28.  
  29.  
  30. // $click_through is the field in your articles table
  31. // $main_ad_cntr is your main ad counter from a table
  32. // $secondary_ad_cntr is your seconday add counter from a table
  33.  
  34.  
  35. //   retrieve from a table containing article-id, main_ad_cntr, secondary_ad_cntr
  36.  
  37. $ad_to_display = which_add($click_trhough, $main_ad_cntr, $secondary_ad_cntr);
  38.  
  39. // you get 1 or 2 ... primary or secondary ad
  40.  
  41. // update the counter accordingly the table containing article-id, main_ad_cntr, secondary_ad_cntr.
  42. You can also use this table to report on actual breakdown of delivered ads 
  43. article_id, main_cnt, secondary_cnt
  44.    1 ,  80 , 20 for a 100 hits total
  45.  
  46. // whenever you change the click_through i would suggest to reset to 0 the cntrs in table containing article-id, main_ad_cntr, secondary_ad_cntr to start fresh
  47.  
  48.  

The sample code is more as pseudo code, i didn't have time to run it but I hope you get the idea of using randomness combined with and adjustment logic to prevent exceeding the target breakdown (from either side main or seconday)..


I hope it helps!

phpNerd01
Sep 8 '08 #2

Atli
Expert 5K+
P: 5,058
phpNerd01, please read your Private Messages.
(Top of the page, under Network)

Thank you.
Sep 9 '08 #3

Atli
Expert 5K+
P: 5,058
As to the problem.

You could also try creating an algorithm that would show a random add based on the ratio you want, but adjust the ratio on-the-fly to reduce the chance that the actual ratio is way of the target ratio.

That could be calculated like so:
Expand|Select|Wrap|Line Numbers
  1. // Calculate a bias ratio.
  2. // This ratio will increase the odds that the next hit corrects
  3. // the current ratio. The further away from the target ratio the
  4. // current ration is, the more this will affect the next hit.
  5. $biasRatio = $targetRatio - ($currentRatio - $targetRatio);
  6.  
  7. // Get a random number between 0 and 1
  8. $random = mt_rand(0, 100) / 100;
  9.  
  10. // Check if the random number falls within the bias ratio.
  11. // If it does, show the primary, if not, show the secondary.
  12. if($random <= $biasRatio) {
  13.     // Show primary
  14. }
  15. else {
  16.     // Show secondary
  17. }
  18.  
You would have to calculate the $currentRatio based on how many times each add has already been shown, and the $targetRatio would be the percentage of times the primary add would be shown.

This would probably not give you the exact ratio you were aiming at, but after like a 100 hits the ratio should balance out. The more hits the more accurate this would be.
Sep 9 '08 #4

P: 9
Hi Guys!!!

Atli and PhpNerd, thank you very much for your assistance!!!

I was in the process of moving homes and I just got my net re-connected again. Sorry I could not check the posts before!!!

I will try to work out the above solutions and see what I get!!!

Atli, the vars $biasRatio and $targetRatio have any values? Or $targetRatio is the actual value from the DB?? How about $biasRatio?


Thanks!!!
Sep 12 '08 #5

Atli
Expert 5K+
P: 5,058
Atli, the vars $biasRatio and $targetRatio have any values? Or $targetRatio is the actual value from the DB?? How about $biasRatio?
The target ratio would be the percentage of times you want your primary add shown (Like say; 0.8 for 80% of the time)

The current ratio is the is the actual ratio, calculated based on how many times the primary add as actually been shown. (primary add shown / total adds shown)
This info would have to be updated and calculated based on data stored in a database.

The bias ratio is the modified target ratio, calculated based on how far away from the target ratio the current ratio is.
This is done so that the final results are as close to the target ratio as possible, while still giving a *randomly generated* list of adds.
Sep 12 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.