【问题标题】:Database query with fullcalendar使用全日历查询数据库
【发布时间】:2015-06-04 15:54:27
【问题描述】:

好的,我正在尝试从 MySQL 数据库中提取事件来填充日历。开始时间以 Unix 时间存储,因此我使用了以下事件源。

events: {
    url: '/php/booking_events.php',
    type: 'POST',
    data: {
       start: start.unix(),
       end: end.unix(),
       branch: branch.id_office,
       instrument: inst
    },
    error: function() {
       alert('there was an error while fetching events!');
    },
}

这带来了第一个问题,当我运行它时,我在开发工具中收到一个错误,说 start is not defined?日历不会自动生成开始和结束时间吗?

其次,如果我在 PHP 中手动输入参数,它会生成一个 JSON 数组,然后将其回显,但脚本一直在说“获取事件时出错!”

    <?php
    require_once('../Connections/localhost.php');
    require_once("../Includes/functions.php");

    //if (!isset($_POST['start']) || !isset($_POST['end'])) {
    //  die("Please provide a date range.");
    //}

    //$range_start = parseDateTime($_POST['start']);
    //$range_end = parseDateTime($_POST['end']);
    //$branch = GetSQLValueString($_POST['id_office'], "int");
    //$inst = GetSQLValueString($_POST['instrument'], "int");

    $range_start = '1433462401';
    $range_end = '1433721599';
    $branch = 2;
    $inst = 3;

    // Parse the timezone parameter if it is present.
    $timezone = null;
    if (isset($_POST['timezone'])) {
        $timezone = new DateTimeZone($_POST['timezone']);
    }

    // Query database to get events
    mysql_select_db($database_localhost, $localhost);
    $query_Events = sprintf("SELECT hm_classes.datetime, hm_classes.id_student, hm_classes.inst FROM hm_classes INNER join hm_rooms ON hm_classes.id_room = hm_rooms.id_room WHERE datetime BETWEEN %s AND %s AND id_office = %s AND inst = %s", $range_start, $range_end, $branch, $inst);
    $Events = mysql_query($query_Events, $localhost) or die(mysql_error());
    while ($row = mysql_fetch_assoc($Events)){
    $id = $row['id_class'];
    $title = 'Booking';
    $start = date('c', $row['datetime']);
    $end = date('c', ($row['datetime'] + hoursToSecods($row['Session'])));
    $input_arrays[]= array(id => $id, title => $title, start => $start, end => $end, allDay =>'false');
}


    // Send JSON to the client.
    echo json_encode($input_arrays);
    ?>

这个的回显结果是

[{"id":"1","title":"Booking","start":"2015-06-05T14:00:00+02:00","end":"2015-06-05T15:00:00+02:00","allDay":"false"}]

我认为 fullcalendar 的目标是什么?任何帮助将不胜感激。

【问题讨论】:

  • 我想你忘了返回结束日期。
  • 它实际上是否也需要结束日期?
  • end 是可选的,但 id 是必需的(id、title、start),而 allDay 和 end 是可选的
  • [{"id":"1","title":"Booking","start":"2015-06-05T14:00:00+02:00","end": "2015-06-05T16:00:00+02:00","allDay":false}]
  • 退货时是否有清单?我只是在查看硬编码事件中需要什么,它们本质上只是标题和开始。

标签: php jquery fullcalendar


【解决方案1】:

好的,我想我已经解决了这个问题,按照 kamlesh.bar 的建议,我去看了http://www.jqueryajaxphp.com/fullcalendar-crud-with-jquery-and-php/

在查看了他的代码后,我将我的 AJAX 请求从主要的完整日历脚本中分离出来,并赋予了它自己的功能。

function getEvents(){
        $.ajax({
            url: 'booking_events.php',
            type: 'POST', // Send post data
            data: {type: 'fetch',
                   branch: $('#branch').val(),
                   inst: $('#instrument').val()},
            async: false,
            success: function(s){
                json_events = s;
            }
        })
    }

然后在全日历中我将事件设置为

events: JSON.parse(json_events),

现在允许将 php 生成的结果输入到日历中。

至于 start: stat.unix() 问题,我只是在 php 中使用 strtotime 将其更改为 Unix 时间格式

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多