I have 3 columns: Day, start_schedule and end_schedule.
Day | start_schedule | end_schedule
-------|-----------------|--------------
Monday | 1:00 | 3:00
Monday | 6:00 | 8:00
Monday | 8:00 | 10:00
I’m still learning php. How will I filter if my input start schedule and end schedule is valid based on the stored times in the database?
For example if I want to add times
- start_schedule =
3:00and end_schedule =7:00
since there is 6:00 - 8:00 you shouldn’t be allowed to add this schedule.
- start_schedule =
7:00and end_schedule =11:00
since there are 6:00 - 8:00 and 8:00 - 10:00 you shouldn’t be allowed to add this schedule.
I would do most of the logic in mysql since it will handle these kinds of expressions very easy and straight forward approaches.
In PHP, before inserting a new entry we will need to check 1 thing:
end_timeless thanstart_time? If so, abort.In MySQL, when trying to insert a new entry we will need to check 4 things:
start_timeinside any existent entry? If so, abort.end_timeinside any existent entry? If so, abort.The above can actually be refactored into one steps:
Sample implementation in PHP querying the database:
Props to @regilero for posting a re-factored version of the where claus required.
This would output: