如何将事件的数据动态添加到fullCalendar


how to dynamically add data for events to fullCalendar?

我的问题是日历中的事件,确切地说,我不知道如何动态添加事件的数据,这是我在视图文件中的代码

<div>
    <?php $message = Message::model()->findAll(); ?>
    <?php foreach($message as $messag): ?>
    <?php $names[] = $messag['contacts']; ?>
    <?php $date[] = $messag['send_datetime']; ?>
    <?php endforeach; ?>
</div>

<script>
$(document).ready(function() {
    // page is now ready, initialize the calendar...
    $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            events: [
            {   
                title: '<?php echo $names[0]; ?>',
                start: '<?php echo $date[0]; ?>',
                url: 'view/1'
            },
                {
                title: '<?php echo $names[1]; ?>',
                start: '<?php echo $date[1]; ?>',
                url: 'view/2'
            },
                {
                title: '<?php echo $names[2]; ?>',
                start: '<?php echo $date[2]; ?>',
                url: 'view/3'
            },
                {
                title: '<?php echo $names[3]; ?>',
                start: '<?php echo $date[3]; ?>',
                url: 'view/4'
                },
        ]
    })
    $('#calendar').fullCalendar('changeView', 'agendaWeek');
});
</script>
<div id='calendar'>
</div>

我不知道如何用这样一种方式对它进行编码,即它会添加和我在数据库中的数据一样多的事件。如果有人能帮助

,我将不胜感激

我将通过ajax加载事件。你基本上有两个部分。一个文件带有datatable/javascript,另一个文件是数据源。它是一个php文件,从数据库中获取所有数据,并以json格式输出事件。

要从文件中获取事件,请使用这样的fullCalendar(简化版):

$('#calendar').fullCalendar({
    events: {url: 'myevents.php'}});

在myevents.php中,您可以进行常见的数据库请求,并像这样输出数据:

<?php
//Do the Database stuff here...
//Here is a sample data for two events:
$events = array();
$agenda['allDay'] = true;
$agenda['start'] = '2014-08-25 12:00:00';
$agenda['end'] = '2014-08-30 12:00:00';
$agenda['title'] = "Hello World";
$agenda['id']= "1";
$events[] = $agenda;
$agenda['allDay'] = false;
$agenda['start'] = '2014-08-27 12:00:00';
$agenda['end'] = '2014-08-27 16:30:00';
$agenda['title'] = "Blah";
$agenda['id']= "2";
$events[] = $agenda;

echo json_encode($events);
exit();