问题
No actual code is required in the answers, only good advices for best practices.
What I need is to display a week timetable (1h timeslots) in a Bootstrap website for booking purposes. The timeslot will have one of the following status:
- Available: green background and 'available' text,
- Already booked: red background and 'occupied' text,
- Not available for booking: gray background and 'not available' text.
When a user clicks on an available timeslot a modal/popup will appear for booking.
Only one week is displayed (= current week). Eventually the user can click on an arrow and view next week timetable. So, I have no need to display more than the current and next week.
I have thought of three possible solutions:
Create a MySQL table with one record for every available timeslot (automatically with a cronjob). This way I can just print the table data in the frontend. Note: I've already done this and works great but not optimal if you have many timetables in the site.
Use a jQuery plugin (like Fullcalendar). Apparently even if what I need is super-simple, Fullcalendar (or other calendar plugins) are not meant for this kind of usage. Even with a lot of customisation, you cannot do this.
Build the timetable dynamically with PHP using the
in_array()
function. The idea is to create two MySQL tables, one for storing non-available hours and the other for reservations.With two simple queries I can obtain 2 arrays (for the week currently displayed): 'non_available_timeslots' and 'reservations'.
When I build the week timetable (normal bootstrap table), for each timeslot I will control 2 things:
a) If the day/hour is in the array of 'non_available_timeslots' (if so
echo '<td class="not-available">Not available</td>'
.....else continue to point b)
b) if the day/hour is in the array of 'reservations' (if so,echo '<td class="booked">Occupied</td>'
.....else continue to point c)
c) if the 2 above conditions are not matched,echo '<td class="available"> Available</td>'
As you can see what I want to do is really simple. I'm interested in solution 3.
From a performance point of view, is it bad? (Note that the absolute max number of elements to search between in the in_array function would be equal to the number of timeslots in the timetable, so 15x7= 105, but in reality probably only 50).
回答1:
From your post you've already decided what solution you'll use. Given this, I would only create one table with a column (status
) that would be different for "Not available" and "Occupied".
I would use only one table because you might need another status in a few days / weeks / months. You can always have more than one array with values from the same table.
I don't think you have to consider performance with that ammount of rows (or timeslots). I say: go with it and adjust as your application grows up.
来源:https://stackoverflow.com/questions/28876427/week-timetable-php-in-array-or-jquery-plugin