简体   繁体   English

从数据库中提取事件到FullCalendar

[英]Fetching events from database into FullCalendar

I am trying to fetch data from a MySQL database into a calendar. 我正在尝试从MySQL数据库中获取数据到日历中。 (FullCalendar plugin) I want to send the fetched data as a json feed to be displayed on the calendar. (FullCalendar插件)我想将获取的数据作为json供稿发送,以显示在日历上。

This is my Javascript code for displaying the events: 这是我显示事件的Javascript代码:

events: {
        url: 'http://localhost/Hotel/event_source.php',
        error: function() {
        alert('There was an error while fetching events.');
        }

This is the event_source.php page where I'm fetching the data from the database: 这是event_source.php页面,在这里我从数据库中获取数据:

<?php
     include('hoteldb.php');
     global $conn;
     if($stmt = $conn->prepare("SELECT title, startDate, endDate FROM event"))
     {
       $stmt->execute();
       $stmt->bind_result($title, $startDate, $endDate);
       while ($stmt->fetch()) 
       {
            $rows[] = array('title' => $title, 'startDate' => $startDate, 'endDate' => $endDate);
       }
       $stmt->close();
       echo json_encode($rows); 
      } 
?>

The code should be modified as followed. 该代码应作如下修改。 The statement fetch method will return the next row and you have to catch it as follows. 语句获取方法将返回下一行,您必须按如下所示进行捕获。 Also the columns are accessed as I have shown in the following code. 正如我在以下代码中所示,还可以访问这些列。

$rows=array();
while ($row=$stmt->fetch()) 
{
    $rows[] = array('title' => $row['title'], 'startDate' => $row['startDate'], 'endDate' => $row['endDate']);
}

Try Below one in your while loop 在while循环中尝试以下

'title' and 'start' must be there in event to load in calendar. 要在日历中加载,“ title”和“ start”必须存在。

while ($stmt->fetch()) 
{
    $rows[] = array('title' => $title, 'start' => date('Y-m-d H:i:s', strtotime($startDate)),  'end' => date('Y-m-d H:i:s', strtotime($endDate)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM