krejzlol
krejzlol

Reputation: 1

how to dynamically add data for events to fullCalendar?

My problem is about events for calendar, precisely speaking i dont know how to dynamically add data for events here is my code in view file

<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>

i dont know how to code it in such a way that it will add as many events as i have data in database. i would be very grateful if someone could help

Upvotes: 0

Views: 1620

Answers (1)

MilMike
MilMike

Reputation: 12831

I would load the events by ajax. You basically have two parts. The one file with datatable/javascript and the other file is the data source. It is a php file that gets all the data from database and outputs the events in json format.

to get the events from a the file use fullCalendar like this (simplified version):

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

and in your myevents.php you make the usual database requests and out put your data like this:

<?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();

Upvotes: 1

Related Questions