0

I have a simple set up in the database such as per below:

|--------------|------------------|--------------|------------|
|    Image     |   Start Month    |  End Month   |   Active   |
|--------------|------------------|--------------|------------|
|  Image1.jpg  |       10         |       3      |      1     | - Show October this year to March next year, every year.
|  Image2.jpg  |        3         |      10      |      1     | - Show March to October, every year.
|  Image3.jpg  |        7         |       5      |      1     | - Show July this year to May next year; every year.
|  Image4.jpg  |        0         |     NULL     |            | - Show this image year round.
|--------------|------------------|--------------|------------|

The idea is to show images based on the month. Every year it's the same so a year is not required. Now the logic of how I am doing this is messing with my mind.

This month it's November (11). $month = 11;

The query I currently have is

 WHERE `start_month` <= $month AND `end_month` >= $month OR `start_month`= 0 ORDER BY RAND()

but that unfortunately only works mid year and doesn't compensate for the images that wrap around into the new year.

What logic do I need to select the images based on the current month $month between the start_month and end_month making it work for those images that cycle through the new year change.

Images with a 0 start month are to be displayed year round.

Dawson Irvine
  • 322
  • 4
  • 19
  • please read https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php and how to prevent from **sql injection** – nbk Nov 22 '21 at 22:20
  • Use parentheses to group the conditions in the `WHERE` clause. You can also use `$month BETWEEN start_month AND end_month` – Barmar Nov 22 '21 at 22:23
  • @nbk I do use prepared statements when user variable data is entered into the database. Since all variable data here is generated via the script `$month = date("m");` there is no risk of SQL injection. – Dawson Irvine Nov 22 '21 at 22:28
  • use for all variables prepared statements. to your problem, moth allone are bad comapsison as you can't detect 10,1,12,1,2,3,4. so must take teh month otpo condsideration. and programm it . Use dates that makes it much easier – nbk Nov 22 '21 at 22:44

0 Answers0