[How to do that] All banners were viewed 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 "banners views should be evenly distributed."
To solve this, basically you create a temporary table that stores the daily views of each banner. Then you increment in this temporary table a counter for each request of that image. At the next banner request, you get the least views. This ensures balance in 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 in the 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 mechanism that disables expired banners, you can schedule a script to run every day at midnight on the server (cron on Linux, Windows Task Scheduler, or an event in the database itself). It will do the following on each of the banner table lines that are active:
Be data_final
the date of inicio
plus the value of dias
;
If data_final
is older than the current date, disable the banner (active = 0).
In addition, for the distribution engine, do the following in the same daily script:
Remove all records in views_banner
;
Enter all active banner ids in views_banner
with column hoje=0
(this column represents the views the banner had on the day).
When you receive a request on link , do the following with the server-side technology of your choice:
Select any record that has the lowest value in the hoje
column (there is no rule to resolve draws);
Increase the value hoje
of this record;
Return the image for this banner.