[英]jQuery Full Calendar Events are not loading from Database in PHP,Codeigniter3
[英]JQuery Full Calendar limit events with PHP
我正在使用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个事件
如果有人有更好的解决方案,请帮忙。
如果您希望每月最多拥有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注入; 仅转义变量是不够的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.