| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| Senior Member Join Date: Sep 2005
Posts: 1,112
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | An in-depth approach to a fair PHP ad randomizer [by Legend2] An in-depth approach to a fair PHP ad randomizer By legend2 The purpose of this tutorial is to shed some light on a way to improve php randomizers. Suppose you got a heavy-traffic website, but a single banner ad spot. Suppose too that you want to allow multiple advertizers to 'share' this space. The biggest question an advertizer would ask here: will my ad get the same share of impressions as others showing on the same spot? The fact is, using random functions to select an arbitrary 'include' is not that fair. No matter what the seed is, and depending on the size/number of possible ads to include, some ad may be favorized more than others. Our aim here is to divide the number of impressions equally among the advertized ads. To give a clear idea of what I'm talking about here, we visualize teh following scenario: We have an '/ads' folder under the root web directory of the website where the ads should show. This folder will contain multiple files, each file correspoding to a different ad. We also assume the web server is running PHP, and a MySQL DB. We start off by creating a table called 'site_ads'. This table will contain two fields: 1) an auto increment field called 'id' which is the primary key for this table. This field will uniquely identify each of the ads. 2) the file name corresponding to the ad, which can be found in the /ads directory. We'll call this field 'name'. The query for this looks as follows: Code: CREATE TABLE site_ads ( id INT NOT NULL AUTO_INCREMENT , name VARCHAR( 100 ) NOT NULL , PRIMARY KEY (id) ) ????: NamePros.com http://www.namepros.com/programming/150761-depth-approach-fair-php-ad-randomizer.html An example of such a query would be: Code: insert into site_ads('',"site1"); as an <img>, but .php used as an example, in case you might need to add custom processing for every ad at an advanced stage). We now reach our key idea: we do have the ads placed, and their corresponding entries in the database. How to divide impressions equally among them? We create a new table in the database called 'counter'. The main purpose of this table is to keep track of the last ad viewed. This will help determining the next ad to display. This table has one field, we call 'current' which will serve the purpose just stated. Note that if 'current' has a value of '5' it means fifth ad, and not the ad with 'id' 5. Why? The reason is that at some point, old ads will be removed, and others will be added. This will result in the 'id' field which is automatically generated (because of its auto increment nature) having 'gaps'. Our approach will take care of this point. The query for our new table is: Code: CREATE TABLE counter ( counter INT NOT NULL, PRIMARY KEY (counter) ) generate. Such a query is: Code: insert into counter values('0'); The following is a detailed PHP code which will illustrate the approch we are following (This is the code to be used in the actual ad space): PHP Code: - connect to the database - fetch the current counter - if counter is not initialized, initialize it to -1 for later increments to 0, and insert it in the database - fetch the total number of ads in the database - set total to 1 in case none exists yet - get the next ad counter by incrementing the current one by 1 and using the math 'modulo %' to limit it to the number of total ads and give it a cyclic nature - select the newcounter'th ad entry - update the new counter location - display the ad file. The above code can be saved in a separate file called ads.php, and we can just include it from another php file. PHP Code: Good Luck. (The code provided in this tutorial has not been tested, feel free to post comments in case you find any bug). Legend2, All rights reserved.
Last edited by legend2; 12-25-2005 at 05:58 PM.
|
| |
| | #2 (permalink) |
| Eating Pie Join Date: Nov 2004 Location: Canada
Posts: 2,272
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Nice Post. If you have mysql 4 or 4.1 I am not 100% sure which one you can always use the RAND command PHP Code: ????: NamePros.com http://www.namepros.com/showthread.php?t=150761 PHP Code: iNod.
__________________ I feel old. |
| |
| | #4 (permalink) |
| Senior Member Join Date: Mar 2005
Posts: 4,948
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Nice post legend. Ad rotation is so simple, yet some people find so hard to do. I find that alot of folks will use something like this: PHP Code: ![]() ????: NamePros.com http://www.namepros.com/showthread.php?t=150761 -Eric
Last edited by SecondVersion; 12-25-2005 at 05:56 PM.
|
| |
| | THREAD STARTER #7 (permalink) |
| Senior Member Join Date: Sep 2005
Posts: 1,112
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | fair would be, if the script is called x times and you have y ads, then each ad would receive floor of x/y. the rand function itself, might conceptually throw x times the same number. and it is more likely to favorise a number on another as the number it can pick from are narrow. in my example, i force the choosing of each ad in a fair fashion. so i a user loads the page at second a displaying ad number 1 then the next user, regardeless of the time frame, will see ad number 2. |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tutorial: Getting started with PHP (The Basics) | deadserious | Webmaster Tutorials | 60 | 11-17-2007 12:35 PM |
| Great Scripts for Sale With Resale Rights! | Zeeble | Scripts For Sale | 20 | 01-04-2006 02:39 AM |
| Googlism - What does google think of you? | deadserious | The Break Room | 55 | 12-15-2005 10:09 AM |