0

I Have a ridiculous problem but even after an entire lurking day I cannot solve it.

I have a Fullcalendar installation and i'm trying to retrieve events data from a file (events.php); i load data via Php from a MySql table.

Problem:

events won't display in my calendar if i generate the feed automatically via Php.

BUT

if i write down in my file the EXACT Json line, it does work properly.

Example:

this is my events.php:

<?php session_start();  

header("Content-Type: application/json");

//if (!isset($id_azienda)) die();

$id_azienda = $_GET['id_azienda'];

// Includo la classe
require_once("./db.class.php");

// Creo l'oggetto database
$db = new DataBase();

// Apro la connessione al database e ne memorizzo il risultato in $open
$open = $db->OpenConnection(); 

$eventiQuery = "SELECT * FROM scadenzario WHERE id_azienda ='$id_azienda'";

// Eseguo la query e ne memorizzo il risultato in $result
$eventiList = $db->Query($eventiQuery);

$numEventi = $db->NumRows($eventiQuery);

// Chiudo la connessione al database e ne memorizzo il risultato in $close
$close = $db->CloseConnection();

/* ************* FINE CARICAMENTO DATI DAL DATABASE ***************** */


while ($record = mysql_fetch_array($eventiList)) {
    $event_array[] = array(
        'id' => $record['id_scadenzario'],
        'title' => $record['titolo'],
        'start' => $record['data_inizio'],
        'url' => $record['url']
    );
}

echo json_encode($event_array);
?>

that will produce:

[{"id":"0","title":"test titolo","start":"2012-09-11","url":"http:\\\/\\\/yahoo.com\\\/"},{"id":"1","title":"test titolo","start":"2012-09-11","url":"http:\\\/\\\/google.com\\\/"}]

and it won't display anything.

But if i copy and paste it into the file "events.php" and comment the echo line:

// echo json_encode($event_array);
?>
[{"id":"0","title":"test titolo","start":"2012-09-11","url":"http:\\\/\\\/yahoo.com\\\/"},{"id":"1","title":"test titolo","start":"2012-09-11","url":"http:\\\/\\\/google.com\\\/"}]

it does works.

EDIT: My fullcalendar implementation is:

var scadenzario = $('#scadenzario-calendario').fullCalendar({

    events: "./events.php",

    header: {

        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'

    },

    selectable: true,
        selectHelper: true,
        select: function(start, end, allDay) {
            var title = prompt('Nome dell\'evento:');
            if (title) {
                scadenzario.fullCalendar('renderEvent',
                    {
                        title: title,
                        start: start,
                        end: end,
                        allDay: allDay
                    },
                    true // make the event "stick"
                );
            }
            scadenzario.fullCalendar('unselect');
        },
        //editable: true,

    theme: true,

    buttonText: {

        today:    'Oggi',
        month:    'Mese',
        week:     'Settimana',
        day:      'Giorno'
    },

    monthNames: ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'],

    monthNamesShort: ['Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago', 'Set', 'Ott', 'Nov', 'Dic'],

    dayNames: ['Domenica', 'Lunedi', 'Martedi', 'Mercoledi', 'Giovedi', 'Venerdi', 'Sabato'],

    dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab']

});

It's kinda foolish, IMHO.

Notes:

  • I read that when i try to parse just one event i can have problem with square brackets: that's not my situation, i'm trying it with two events;
  • I first thought about character encoding, but i've tried text/plain and application/json, both with no luck;
  • the surplus of slashes in the url is due to a lack of Gn'R sleaze hard rock style in the past few years. Lol XD (I mean: that's not the point)
  • UPDATE: I tried also to create a txt with fwrite and write the json string into the file. No luck. The only way to get the work done seems to write the code by hand into the file. Well, i might consider it as a job offer, maybe. Lol.

Any ideas? Thank you in advance :)

P.s.: Sorry for italian comments and variables in the code, but that's not the point ;)

Yuri Refolo
  • 245
  • 9
  • 17

2 Answers2

0

I think it's a json_encode Problem. It got changed or updated on the recent php versions 5.3 and 5.4. Which version are you running on? Try to figure out what values are in your $event_array. One thing i would also try is to utf8_encode all the values from the database. Can't really help you without firebug or wireshark debugging.

while ($record = mysql_fetch_array($eventiList)) {
  $event_array[] = array(
    'id' => utf8_encode($record['id_scadenzario']),
    'title' => utf8_encode($record['titolo']),
    'start' => utf8_encode($record['data_inizio']),
    'url' => utf8_encode($record['url']));
}

EDIT: LOL this should not work btw. $event_array is a variable that only exists in the while function. So define it in the global scope!

LANopop
  • 13
  • 4
  • Thank you for your reply: I'm using php 5.3.3. I've already tried utf8 _encoding, alas. But with Firebug Lite for Chrome i've noticed that there is a problem with GET ./events.php?start=1345932000&end=1349560800&_=1347709880459 Params _ 1347709944871 end 1349560800 start 1345932000 Response: null – Yuri Refolo Sep 15 '12 at 11:54
  • $event_array is not the problem, it does write down the entire string with no problem... ;) – Yuri Refolo Sep 15 '12 at 11:59
  • did you try defining the $event_array outside the while scope? But you dont check for GET or POST on the event.php, so normally you should just get the result... – LANopop Sep 15 '12 at 12:02
  • A while loop doesn't create a new scope, as you can see here: http://codepad.org/H1U3wXZD ... but thank you for the try ;) – Yuri Refolo Sep 15 '12 at 12:48
  • I also tried to write the json string without json_encode, indeed. The problem remains the same. – Yuri Refolo Sep 15 '12 at 13:32
0

Try adding on the top of your php script

header("content-type: application/json"); 

I think the problem is a missing header declaration that on some webserver may cause a different interpretation. Also try changing the format of your php file (the script you use to serve the events to the calendar of course) to an utf-8 charset (I use notepad++ as editor -> Format -> utf-8 encode format)

Finger cross

bogatyrjov
  • 5,317
  • 9
  • 37
  • 61
Marco
  • 29
  • 1