473,322 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
5 1570
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
5,058 Expert 4TB
phpNerd01, please read your Private Messages.
(Top of the page, under Network)

Thank you.
Sep 9 '08 #3
Atli
5,058 Expert 4TB
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
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
5,058 Expert 4TB
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

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

Similar topics

21
by: Nik Coughlin | last post by:
Are there methods for manipulating images in JavaScript that would allow me to write functions to rotate, skew, mask and resize images (bitmaps)? The functions need to be fast enough for use in a...
3
by: steve | last post by:
I have a tablet PC and I write a application on this tablet PC, I would like to rotate the screen from portrait<->landscape mode(or 90/180/270 degree), what should I do? I only find the code for...
7
by: Showjumper | last post by:
Hi, I have developed an upload server controls to be reused over a number of projects. One of the tasks it needs to handle is to rotate an image. I want to accoplish this by checking the checkbox...
3
by: byrd48 | last post by:
Hi, I am developing a web site which allows users to upload and share photos. I have a datalist which lists the photos and has the usual edit, update commands. Within the edititemtemplate, I...
17
by: santel_helvis | last post by:
Hi All, Could anyone tell me how to rotate the image in javascript. Which concepts I should concentrate to rotate the image
0
by: neoteny2 | last post by:
I need MS Access to automatically create reports/subreports based on specific criteria. I am building a database in Access 2003 with different locations/sites. I have the "sites" table created...
8
by: lovecreatesbeauty | last post by:
I write a function to rotate a matrix by 90 degrees clockwise, this function works on a matrix of specific size, for example, it rotates a 4*4 matrix of integers in the following code. The function...
13
by: =?Utf-8?B?dmlubw==?= | last post by:
Hello, I have created my proper control and i would to rotate it. In the paint event of my object, i have written : Dim graph As Graphics = myObject.CreateGraphics myMatrix = New...
0
by: harryusa | last post by:
I am experimenting with the rotate function and so far I can't get my code to return anything but the URL of my script. Here it is: <?php // The file you are rotating $image =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.