FullCalendar - 仅在日期之间加载事件

时间:2015-10-30 19:23:00

标签: php jquery sql fullcalendar

抱歉我的英语! 我正在使用带有sql和php的FullCalendar。 问题是我在数据库中有很多事件(超过4000),我只想加载实际的周事件,然后点击下一个/上一个按钮再次加载本周的事件......

我的.js是:

$('#calendar').fullCalendar({
    defaultView:'agendaWeek',
    events: base+"v.php?acction=start",
    .....

我在v.php中的查询是:

$query_visitas ='SELECT *,concat_ws(" ",name,surname) as title, 
visit_id as id, concat_ws(" ",date_start,time_start) as start,
concat_ws(" ",date_end,time_end) as end FROM visits v 
LEFT JOIN pacient p ON v.pacient_id = p.id 
ORDER BY START'; 

感谢您的建议,因为我疯了!

1 个答案:

答案 0 :(得分:1)

使用JSON从服务器获取FullCalendar事件时,显示的日历的startend日期随请求一起发送。有关文档,请参阅 events as a JSON feed

您需要使用这些值并使用它们来过滤SELECT返回的结果。它有点棘手,因为我们需要找到以下行:

  • start之后和end之前结束时间。
  • start之后和end之前有一个开始时间。
  • start之前有一个开始时间,在end之后有结束时间。

您需要使用CONCAT_WS()来形成有效的开始和结束日期时间字符串,然后使用DATE将其转换为正确的STR_TO_DATE()数据类型。

// FullCalendar V1 sends timestamps
$start =  isset($_REQUEST['start'])? intval($_REQUEST['start']) : 0;
$end =  isset($_REQUEST['end'])? intval($_REQUEST['end']) : 0;

// FullCalendar V2 sends ISO8601 date strings
$start =  isset($_REQUEST['start'])? strtotime($_REQUEST['start']) : 0;
$end =  isset($_REQUEST['end'])? strtotime($_REQUEST['end']) : 0;

// convert the timestamps to date strings for the SQL
$start_date = date('Y-m-d', $start);
$end_date = date('Y-m-d', $end);

// ordinarily you would use a prepared statement, but since you didn't specify a driver they variables are included inline - should be sanitized by date()`
$sql = <<<SQL
    SELECT *, 
        visit_id as id, 
        CONCAT_WS(' ', name, surname) as title, 
        CONCAT_WS(' ', date_start, time_start) as start,
        CONCAT_WS(' ', date_end, time_end) as end 
    FROM visits v 
    LEFT JOIN pacient p 
        ON v.pacient_id = p.id 
    WHERE
        -- anything with an end between the start/end
        STR_TO_DATE(CONCAT_WS(' ', date_end, time_end), '%Y-%m-%d %h:%i:%s') BETWEEN '{$start_date}' AND '{$end_date}'
        OR 
        -- anything with an end between the start/end
        STR_TO_DATE(CONCAT_WS(' ', date_start, time_start), '%Y-%m-%d %h:%i:%s') BETWEEN '{$start_date}' AND '{$end_date}'
        OR 
        -- anything with a start before the start and an end after the end
        (
            STR_TO_DATE(CONCAT_WS(' ', date_start, time_start), '%Y-%m-%d %h:%i:%s') < '{$start_date}' 
            AND STR_TO_DATE(CONCAT_WS(' ', date_end, time_end), '%Y-%m-%d %h:%i:%s') > '{$end_date}'
        )
    ORDER BY start, end
SQL;