Fullcalendar PHP mysql JSON


Fullcalendar PHP mysql JSON

大家好,我正在开发一个带有规划模块的应用程序我正在使用Fullcalendar JS构建plannig日历我发现这个代码

initCalendar: function () {
            if (!jQuery().fullCalendar) {
                return;
            }
            var date = new Date();
            var d = date.getDate();
            var m = date.getMonth();
            var y = date.getFullYear();
            var h = {};
            if ($('#calendar').width() <= 400) {
                $('#calendar').addClass("mobile");
                h = {
                    left: 'title, prev, next',
                    center: '',
                    right: 'today,month,agendaWeek,agendaDay'
                };
            } else {
                $('#calendar').removeClass("mobile");
                if (App.isRTL()) {
                    h = {
                        right: 'title',
                        center: '',
                        left: 'prev,next,today,month,agendaWeek,agendaDay'
                    };
                } else {
                    h = {
                        left: 'title',
                        center: '',
                        right: 'prev,next,today,month,agendaWeek,agendaDay'
                    };
                }               
            }
            $('#calendar').fullCalendar('destroy'); // destroy the calendar
            $('#calendar').fullCalendar({ //re-initialize the calendar
                disableDragging: false,
                header: h,
                editable: true,
                events: [{
                        title: 'All Day Event',                        
                        start: new Date(y, m, 1),
                        backgroundColor: App.getLayoutColorCode('yellow')
                    }, {
                        title: 'Long Event',
                        start: new Date(y, m, d - 5),
                        end: new Date(y, m, d - 2),
                        backgroundColor: App.getLayoutColorCode('green')
                    }, {
                        title: 'Repeating Event',
                        start: new Date(y, m, d - 3, 16, 0),
                        allDay: false,
                        backgroundColor: App.getLayoutColorCode('red')
                    }, {
                        title: 'Repeating Event',
                        start: new Date(y, m, d + 4, 16, 0),
                        allDay: false,
                        backgroundColor: App.getLayoutColorCode('green')
                    }, {
                        title: 'Meeting',
                        start: new Date(y, m, d, 10, 30),
                        allDay: false,
                    }, {
                        title: 'Lunch',
                        start: new Date(y, m, d, 12, 0),
                        end: new Date(y, m, d, 14, 0),
                        backgroundColor: App.getLayoutColorCode('grey'),
                        allDay: false,
                    }, {
                        title: 'Birthday Party',
                        start: new Date(y, m, d + 1, 19, 0),
                        end: new Date(y, m, d + 1, 22, 30),
                        backgroundColor: App.getLayoutColorCode('purple'),
                        allDay: false,
                    }, {
                        title: 'Click for Google',
                        start: new Date(y, m, 28),
                        end: new Date(y, m, 29),
                        backgroundColor: App.getLayoutColorCode('yellow'),
                        url: 'http://google.com/',
                    }
                ]
            });
        },

因此,我制作了一个php脚本,以JSON格式返回事件

  <?php
require_once("inc/db_const.php"); 
try {
    $connection= new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    $query = "SELECT id,title,start,end FROM events";
    $sth = $connection->prepare($query);
    $sth->execute();
    $events = array();
 $sth->bind_result($id,$title,$start,$end);
 while($sth->fetch()) {
        $e = array();
        $e['id'] = $id;
        $e['title'] = "Lorem Ipsum";
        $e['start'] = $start;
        $e['end'] =$end;
        $e['allDay'] = false;
        // Merge the event array into the return array
        array_push($events, $e);
    }
    // Output json for our calendar
    echo json_encode($events);
    exit();
} catch (PDOException $e){
    echo $e->getMessage();
}
 ?>

有人能帮助我如何将fullcalnder的代码从静态值更改为从php返回的JSON吗

只需将PHP脚本的URL传递给Events参数,而不是事件数组:

       $('#calendar').fullCalendar({ //re-initialize the calendar
            disableDragging: false,
            header: h,
            editable: true,
            events: "/url/to/your/PHP/Script.php"
        });

有关详细信息:http://fullcalendar.io/docs/event_data/events_json_feed/