我正在使用JQuery完整的日历活动。我知道到目前为止还没有一个选项会按限制加载事件。
在月视图中,如果我在1天内有100个事件,则会使表格单元格变得非常大。我想将其限制为每天10个事件。
我认为我会做的是使用PHP在服务器端执行此操作。由于我在给定日期范围的情况下使用PHP加载所有事件,我想我会每天加载并将其限制为每天10个事件。
这就是我在PHP方面所拥有的:
if ($currView == 'month') //curr view is the current view of the calendar
{
$start = $_GET['start']; //gets from the calendar usually start of month
$end = $_GET['end']; //gets from calendar usually end of month
//some array to store all events before using json_encode
$limitEvents = array();
$limit = 10;
//loop through from start till end by 1 day incremental
for ($i = $start; $i < $end; $i+=strtotime($i, "+1 day"))
{
//make the end of day to be the current day but 23:59:59
$endOfDay = date('m/d/Y', $start);
$endOfDay = strtotime($endOfDay . ' 23:59:59');
//load the events from DB using date range which is 1 day and limit of events
$loaded = $this->loadDataFromDB($start, $endofDay, $limit);
//merge the arrays
array_merge($limitTasks, $loaded);
}
}
现在这个代码的问题在于,当我尝试运行它时它不是最佳的,它每天循环遍历并且每天查询并且需要很长时间,甚至超时。
我的问题是,我如何在MySQL方面做到这一点?我会给出从月初到月末的日期范围。而不是每天在PHP端循环并每天加载数据,我会在MySQL方面这样做。例如,查询看起来像是:
SELECT *
FROM Events_Table
WHERE Ev_Date BETWEEN :startMonth AND :endMonth
(have extra query to load by days within given start and end month
and have LIMIT 10 per day)
无法完成上述查询,括号中的文本需要在有效查询中传输,以便每天选择10个事件,并且有31天所以它应该选择大约310个事件
如果有人有更好的解决方案,请帮助。
答案 0 :(得分:1)
如果您希望每月最多10个,请在下面进行更改,以便在循环播放时减少限制...
$currView == 'month') //curr view is the current view of the calendar
{
$start = $_GET['start']; //gets from the calendar usually start of month
$end = $_GET['end']; //gets from calendar usually end of month
//some array to store all events before using json_encode
$limitEvents = array();
$limit = 10;
//loop through from start till end by 1 day incremental
for ($i = $start; $i < $end; $i+=strtotime($i, "+1 day"))
{
//make the end of day to be the current day but 23:59:59
$endOfDay = date('m/d/Y', $start);
$endOfDay = strtotime($endOfDay . ' 23:59:59');
//load the events from DB using date range which is
//1 day and limit of events
if ($limit>0){ //if you want to have 10 in total add this
$loaded = $this->loadDataFromDB($start, $endofDay, $limit);
$limit -= count($loaded); //and this
} //and this
//merge the arrays
array_merge($limitTasks, $loaded);
}
并为您的mysql查询限制实际获得的金额
SELECT *
FROM Events_Table
WHERE Ev_Date BETWEEN :startMonth AND :endMonth
LIMIT 10
然后,如果您仍然放慢速度,请检查数据库中是否有适合您字段的索引。当您在where子句中使用开始月份和结束月份时,当您为每个
添加索引时,它们将查询得更快另外,不确定您使用什么来运行查询,但要确保在占位符周围添加单引号或者仍然可以进行mysql注入;只是逃避变量是不够的