Pedro
Pedro

Reputation: 89

Retrieve data events fullcalendar json

I'm trying to figure out how to put json data in fullcalendar. I've searched for many tutorials and foruns and I didn't find a solution. I am new to jquery and ajax and json.

Code getEvents.php:

<?php

$event = array('id'=>99, 'title' => 'aaaaa','start' => '2015-12-15');


echo json_encode($event);

Code jquery:

$('#calendar1').fullCalendar({
    height: 450,
    monthNames: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
    dayNamesShort: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'],
//    weekends: false,
    firstDay: 1,
    defaultDate: moment('2015-12-01'),

    dayClick: function (date, jsEvent, view) {

        var data = date.format();

        if (date.toDate().getDay() != 6 && date.toDate().getDay() != 0) {


            if (dias_ocupados > dias_disponiveis - 1) {

                //  console.log('Já esgotou os seus dias de férias');

            } else {
                // verifica se a data ja ta ocupada 
                if ($.inArray(data, datas_ocupadas_total) == -1) {

                    adicionaEvento(count_events, sigla, date, cor_picker, '#calendar1');

                    //   console.log('Dias ocupados: ' + dias_ocupados + ' ' + datas_ocupadas_total[0]);

                } else {

                    // remove event

                }
            }
        }
    },
    eventClick: function (calEvent, jsEvent, view) {
        var data = calEvent.start.format();
        removeEvento(calEvent.id, data, '#calendar1');
        //  alert('Event: ' + calEvent.start.format()+ ' '+count_events);
        //  removeEvento(calEvent.id,data,'#calendar1');
        //    $('#calendar1').fullCalendar("removeEvents",calEvent.id);

    },
     eventSources: {
       url: '/gestor/Controller/getEvents.php',
       color: 'black'
   }

}); 

Upvotes: 0

Views: 283

Answers (1)

smcd
smcd

Reputation: 3265

The event JSON is an array of objects. It should look like

[
    {
        id: 99,
        title: 'aaaaa',
        start: '2015-12-15'
    },
    /* ... */
]

For your PHP code this should get you there

<?php

$events = array(
    array('id'=>99, 'title' => 'aaaaa','start' => '2015-12-15'),
    array('id'=>98, 'title' => 'bbbbb','start' => '2015-12-16')
);

echo json_encode($events);

Upvotes: 1

Related Questions