问题描述
在答案中不需要任何实际代码,只是对最佳做法的良好建议。
我需要的是在Bootstrap中显示一周的时间表网站预订用途。时间槽将具有以下状态之一:
- 可用:绿色背景和可用文本,
- 已预订:红色背景和占用文字,
- 不可用于预订:灰色背景和不可用文字。
当用户点击可用的时间段时,会出现一个模态/弹出窗口供您预订。
只显示一周(=当前周)。最终,用户可以单击箭头并查看下周的时间表。
我已经想到了三种可能的解决方案:
-
创建一个MySQL表,每个可用时段有一个记录(使用cronjob自动创建)。这样我可以在前端打印表数据。
jQuery插件(如Fullcalendar)。显然,即使我需要的是超简单,Fullcalendar(或其他日历插件)不是为这种用法。 -
使用
in_array()函数。
这个想法是创建两个MySQL表,一个用于存储不可用小时,另一个用于保留。
通过两个简单的查询,我可以获得2个数组(对于当前显示的周):'non_available_timeslots'和'reservations'。
当我建立星期时间表(正常的启动表),对于每个时间段,我将控制2件事:
a)如果日/ 'non_available_timeslots'的数组(如果是这样
echo'< td class =not-available> / td>'
..... else继续指向b)
d)如果日期/小时在预订的数组中(如果是,则echo'< td class = booked> Occupied< / td>'
..... else )
c)如果上述2个条件不匹配,echo'< td class = available>可用< / td>'
看看我想做的是真的很简单。 我对解决方案3感兴趣
。
从性能的角度来看,是不是? (请注意,在in_array函数中要搜索的元素的绝对最大数量将等于时间表中的时隙数,因此15x7 = 105,但实际上可能只有50)。
解决方案从你的帖子你已经决定了你会使用什么解决方案。鉴于此,我只创建一个表,其中有一列( status
),对于不可用和被占用不同。
我只使用一个表格,因为您可能需要几天/周/月的其他状态。你总是可以有多个数组的值来自同一个表。
我不认为你必须考虑性能与行数(或时间)。我说:随着你的应用程序的成长,与它一起调整。
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).
解决方案 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.
这篇关于周时间表:PHP in_array或jQuery插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!