Hi all
I have a table of items and a table of bookings; each item may have a number of bookings. I want to express the booking status of all items simply in a list; users will click to get detail.
All dates are in YYYY-MM-DD format.
If an item is currently booked, I could use a function like this:
$today = date('Y-m-d');
if ($startDate < $today && $endDate > $today){ // item is currently booked
$status = "Item is booked until ".$endDate;
}
If an item is booked soon, I could do:
if ($startDate > $today){ // item has a booking to come
$status = "Item is booked from ."$startDate;
}
There could be 10 bookings for each item. Validation on the bookings means they don't overlap. However, how do I get the list of bookings from the MySQL database in the most effective way?
I don't want to keep historical bookings, so I think the most logical approach would be to first, delete any bookings whose endDate has passed, then get the list of all bookings ordered by startDate, LIMIT 1. That'll get me the closest booking to today that hasn't finished yet.
I can then check if $today is inside the range of that booking and show 'booked until' or 'booked from' as above.
I would then query the database again to see if there is more than one booking for the item, so I can then also show a total quantity of bookings with a link to see them all.
Is there a better way of doing this?
Many thanks