Question

I have created a banner ad mechanism for a website. Banner clicktag, url etc. are stored in mysql database. At the moment I'm just getting random banner from all available and display it on a website. But how can I implement some sort of mechanism so that all banners get displayed evenly but in random order?

Was it helpful?

Solution

Add another column to your banners table that will count the number of times a banner was used. Let's say you will call it "banner_usages".

I guess that you are getting the random banner using a select, ordering by the rand() function and limiting the number of results to 1.

So given the new column you should only add a new sort field, the new column, as being the first one used

So if you will sort the results using the number of usages ascending you will have the ones that are not used as much at the start. For the ones with the lowest number of usages you will order them by rand() so you will get that effect.

So your query should be something like:

SELECT * FROM `banners` ORDER BY banner_usages ASC, RAND() LIMIT 1

Also remember right after the select to update the number of usages. Something like

UPDATE `banners` SET `banner_usages` = `banner_usages` + 1 WHERE `banner_id` = _THE_ONE_YOU_JUST_GOT_

That should be it

OTHER TIPS

Add a counter column to your SQl table to track how many times each add has been viewed and use

SELECT ad_url FROM ads ORDER BY counter LIMIT 1;

Assuming the ads aren't to be served to one user exclusively this will appear to be random to the user, since there single add view will affect what the next user sees, will maintaining equal distribution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top