I'm working on building an ad banner rotation script based on impressions that displays ads evenly throughout the month. The calculations will be done each time the ad is requested to be displayed. So this will be done on the fly. The ads should appear to rotate through, one after another, and not just display one ad for 1000 impressions, then the other ad for 1000 impressions. It for the most part should display for 1 impression, then switch ads (unless of course one ad has a lot more impressions than the other to use up).
Let's say I have 5 ads and each has a different number of impressions that were purchased, what would be the formula/how do you serve up the ads? I'm looking to do this in PHP.
Ad #1: 1,000 purchased impressions
Ad #2: 12,000 purchased impressions
Ad #3: 3,000 purchased impressions
Ad #4: 20,000 purchased impressions
Ad #5: 10,000 purchased impressions
if there are multiple ads that bought 1000 impressions all for the same time frame, it should show one after another until the impressions are used. Though, I think it might be good that if a person bought 1000 impressions for a short time frame, I should account for that and show them at a faster rate. I'm open to suggestions.
I think you should use the best type of algorithm for the best JOB i'll only show you some few possibility on how to implement such
My current example would show using
You can also implement
Simple Prove of Concept
Simple Shuffle Function that can be used
Classes Used
As you can see this is an example .... Just try and make your solution flexible
Personally, I'd work out what percentage of impressions each ad has received compared to how many are paid for, and use that as the chance that it won't show up. Something like this:
For example, consider four ads, A, B, C and D. All of them have paid for 1,000 impressions, but so far A has been unlucky and gotten zero, while B and C both have had 500 impressions, and D has had 999.
This would mean
$show
has these values for the ads:$total
is therefore equal to 201.$rand
can be any number from 1 to 201 inclusive. Let's say 141.In this case, we begin our loop:
$rand -= 100
, now it's 41. 41 is truthy and we have ads remaining.$rand -= 50
, now it's -9. It has reached zero, so end the loop.The
$winner
is 1, which is advert B.