无法将MySQL中的数据看到fullcalendar

时间:2016-03-13 09:16:02

标签: php jquery fullcalendar

修改

是否可以将json数据发送到此部分:

 $.each( res, function(key, row)(
        {
        events: [
        {
          title: 'All Day Event',
          start: //from AJAX row['name'],
          backgroundColor: "#f56954", //red
          borderColor: "#f56954" //red
        },
...

我正在尝试将MysQL中的数据添加到fullcalendar

这是我的fullcalendar脚本:

     <script>
        (function ($) { 
            $(document).ready(function() {

      $('#calendar').fullCalendar({
          header: {
              left: 'prev,next today',
              center: 'title',
              right: 'month,agendaWeek,agendaDay'
          },
          editable: true,
          eventLimit: true, // allow "more" link when too many events
          events: {
              url: 'fullcalendar/get-events.php',
              //url: 'fullcalendar/myfeed.php',
              error: function() {
                  $('#script-warning').show();
              }
          },
          loading: function(bool) {
              $('#loading').toggle(bool);
          }
      });

  });
        })(jQuery);
    </script>

我从get-events.php获取值,这里是代码:

<?php
//Set error reporting on
error_reporting(E_ALL);
ini_set("display_errors", 1);

//Include connection file
require_once('global.php');

//Json and PHP header
header('Content-Type: application/json');
$events = array();
$user = $_SESSION['username'];
$id_logged = $_SESSION['login_id'];

    $search_date = "SELECT * FROM appointment WHERE id_logged = :id_logged";
    $search_date_stmt = $conn->prepare($search_date);
    $search_date_stmt->bindValue(':id_logged', $id_logged);
    $search_date_stmt->execute();
    $search_date_stmt_fetch = $search_date_stmt->fetchAll();
    $search_date_stmt_count = $search_date_stmt->rowCount();

    $i = 0;
    foreach($search_date_stmt_fetch as $row)
    {
        $events[$i] = $row;
        $i++;
    }

    echo json_encode($events);
?>

结果在XHR上正确显示:

enter image description here

enter image description here

但我在日历中看不到任何一个:

enter image description here

1 个答案:

答案 0 :(得分:1)

按照医生说的那样建立你的回复:

$search_date_stmt_fetch = $search_date_stmt->fetchAll();
$search_date_stmt_count = $search_date_stmt->rowCount();

foreach($search_date_stmt_fetch as $row)
{
    $events[] = array(
       'start' => $row->your_date_start,
       'end'   => $row->your_date_end,
       'id'    => $row->your_id
       ...);
}

echo json_encode($events);

请参阅文档以了解所需的属性:http://fullcalendar.io/docs/event_data/Event_Object/