[How to make it] All banners were seen the same amount of times;
This would only be possible if there were a number of banner views multiple of the total number of banners, which is not practical in reality. I’m interpreting this as "banner views should be evenly distributed".
To solve this, you basically create a temporary table that stores the daily views of each banner. Then you increment, in this temporary table, a counter to each request of this image. In the next banner request, you take the one with the least views. This ensures balance in the distribution.
To remove expired banners, check at the beginning of each day if it is still valid.
Detailing a little more:
You can leave your structure on DB more or less like this:
banner
| id | nome | link | imagem | inicio | dias | ativo |
|----+------+------+--------+------------+------+-------|
| 0 | ... | ... | ... | 2017-01-28 | 10 | 1 |
| 1 | ... | ... | ... | 2017-01-31 | 3 | 0 |
| 2 | ... | ... | ... | 2017-02-02 | 20 | 1 |
views_banner
| id | hoje |
|----+------|
| 0 | 3 |
| 2 | 2 |
For the engine that disables expired banners, you can program a script to run every day at midnight on the server (cron on Linux, Task Scheduler on Windows, or an event in the database itself). It will do the following on each of the banner table rows that are active:
- Be it
data_final
the date of inicio
plus the value of dias
;
- If
data_final
is older than the current date, disables the banner (active=0).
Also, for the distribution mechanism, do the following in the same daily script:
- Remove all records on
views_banner
;
- Enter all active banner ids in
views_banner
with the column hoje=0
(this column represents the views the banner had on the day).
Upon receiving a request on http://site.com/banner_rotativo, do the following with server-side technology of your preference:
- Select any record that has the lowest value in the column
hoje
(lack rule to solve ties);
- Increment the value
hoje
of that record;
- Return the image for this banner.
Not using "random views" means that banners should appear in a specific order?
– rodorgas