jquery 日历从 mysql 数据库中提取信息

jquery calendar pull information from mysql database

我有一个 jquery 日历,我想用 mysql 数据库中的数据填充它,我的 php 脚本独立运行,即正确显示数据,但我正在努力让它与日历脚本一起使用。有人有什么想法吗?

这是我的代码;-

<script>


$(document).ready(function() {

    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicWeek,basicDay'
        },
        defaultDate: '<?php echo date("Y/m/d");?>',
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        events: [
            {
                id: 999,
                title: 'Repeating Event',
                start: '2014-11-16T16:00:00'
            },

            {
                title: 'name',
                start: '2015-04-27',
                end:   '2015-04-29',
            },

            {
                title: 'Michael',
                start: '2015-04-27',
                end:   '2015-04-27',
            },
            {
                title: 'Matthew',
                start: '2015-04-27',
                end:   '2015-05-01',
            },
            {
                title: 'Eric',
                start: '2015-05-04',
                end:   '2015-05-08',
            }
        ]
    });

});

php include('../include/connect.php');

$result = mysql_query("SELECT * FROM holidays ORDER BY id DESC");
or die(mysql_error()); 

 while($row = mysql_fetch_array($result))
 {

echo "  {   title:'" . $row['title'] . "',";
echo "      start:'" . $row['start'] . "',";
echo "      end:'" . $row['end'] . "'},";
 }

您应该停止使用 mysql 方法,现在已弃用,请改用 PDO 或 mysqli 方法。

我认为你的 ORDER BY id 在这种情况下非常无用,因为它只会加载所有事件,我们并不关心它是如何排序的

要创建json,会是这样的:

$json = array();
while($row = mysql_fetch_array($result)) {
      array_push($json, array("title" => $row['title'],
                              "start" => $row['start'],
                              "end" => $row['end']));
}

header('Content-Type: application/json');
echo json_encode($json);

在你的 fullcalendar 文件夹中,你有一个从 JSON 加载事件的例子,如果我记得

您缺少 events 作为 json 供稿。 click for docs

JS:

$('#calendar').fullCalendar({
    events: '/myfeed.php'//place url to your php script here.
});

PHP:

php include('../include/connect.php');
$result = mysql_query("SELECT * FROM holidays ORDER BY id DESC") or die(mysql_error()); 
$events = array();
while($row = mysql_fetch_array($result))
 {
    $events[] = array(
                    "title" => $row['title'],
                    "start" => $row['start'],
                    "end" => $row['end']
                );
 }
echo json_encode($events);