【问题标题】:JQuery Full Calendar limit events with PHPJQuery Full Calendar 使用 PHP 限制事件
【发布时间】:2013-01-09 18:22:17
【问题描述】:

我正在使用 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 条或用于开始和结束边界的 where 子句?
  • 它没有给我带来麻烦,我无法完成查询,因此它每天选择 10 个事件,并且有 31 天,所以它应该选择大约 310 个事件
  • 您是否关心按时间顺序获得前 10 个?或者只是你在某一天得到的不超过 10 个?
  • 没关系,只要我只得到 10 个

标签: php mysql sql fullcalendar


【解决方案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 注入;仅仅转义变量是不够的

【讨论】:

  • 但这不是我想要达到的目标,我每个月的每一天每天都需要 10 个事件。所以如果一个月有 30 天,我需要 30 * 10 个事件或 300 个事件。上面的代码很慢,我正在使用 PDO
  • 是的,正如我在回答中提到的那样,只需使用第二个即可;将 LIMIT 10 添加到您的查询中,并将索引添加到 startMonth 和 endMonth
猜你喜欢
  • 1970-01-01
  • 2015-01-13
  • 2013-06-15
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多