Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database query with fullcalendar

OK I'm trying to pull events from a MySQL database to populate a calendar. The start times are stored in Unix time so I have used the following events source.

events: {
    url: '/php/booking_events.php',
    type: 'POST',
    data: {
       start: start.unix(),
       end: end.unix(),
       branch: branch.id_office,
       instrument: inst
    },
    error: function() {
       alert('there was an error while fetching events!');
    },
}

This brings up the first problem, when I run this I get an error in dev tools saying start is not defined? Doesn't the calendar automatically generate the start and end times?

Secondly, if I manually enter parameters into my PHP it generates a JSON array then echoes it back but the script is constantly saying 'there was an error while fetching events!'

    <?php
    require_once('../Connections/localhost.php');
    require_once("../Includes/functions.php");

    //if (!isset($_POST['start']) || !isset($_POST['end'])) {
    //  die("Please provide a date range.");
    //}

    //$range_start = parseDateTime($_POST['start']);
    //$range_end = parseDateTime($_POST['end']);
    //$branch = GetSQLValueString($_POST['id_office'], "int");
    //$inst = GetSQLValueString($_POST['instrument'], "int");

    $range_start = '1433462401';
    $range_end = '1433721599';
    $branch = 2;
    $inst = 3;

    // Parse the timezone parameter if it is present.
    $timezone = null;
    if (isset($_POST['timezone'])) {
        $timezone = new DateTimeZone($_POST['timezone']);
    }

    // Query database to get events
    mysql_select_db($database_localhost, $localhost);
    $query_Events = sprintf("SELECT hm_classes.datetime, hm_classes.id_student, hm_classes.inst FROM hm_classes INNER join hm_rooms ON hm_classes.id_room = hm_rooms.id_room WHERE datetime BETWEEN %s AND %s AND id_office = %s AND inst = %s", $range_start, $range_end, $branch, $inst);
    $Events = mysql_query($query_Events, $localhost) or die(mysql_error());
    while ($row = mysql_fetch_assoc($Events)){
    $id = $row['id_class'];
    $title = 'Booking';
    $start = date('c', $row['datetime']);
    $end = date('c', ($row['datetime'] + hoursToSecods($row['Session'])));
    $input_arrays[]= array(id => $id, title => $title, start => $start, end => $end, allDay =>'false');
}


    // Send JSON to the client.
    echo json_encode($input_arrays);
    ?>

The echoed result of this is

[{"id":"1","title":"Booking","start":"2015-06-05T14:00:00+02:00","end":"2015-06-05T15:00:00+02:00","allDay":"false"}]

which is what I think fullcalendar is after? Any help would be greatly appreciated.

like image 209
Aaron Harker Avatar asked Nov 10 '22 12:11

Aaron Harker


1 Answers

OK I think I have solved this problem, following kamlesh.bar's suggestion I went to look at http://www.jqueryajaxphp.com/fullcalendar-crud-with-jquery-and-php/.

After looking through his code I separated my AJAX request out from the main fullcalendar script and gave it it's own function.

function getEvents(){
        $.ajax({
            url: 'booking_events.php',
            type: 'POST', // Send post data
            data: {type: 'fetch',
                   branch: $('#branch').val(),
                   inst: $('#instrument').val()},
            async: false,
            success: function(s){
                json_events = s;
            }
        })
    }

Then in fullcalendar I set the events as

events: JSON.parse(json_events),

This is now allowing the results generated by the php to be entered into the calendar.

As for that start: stat.unix() issue, I am just using strtotime in php to change that to a Unix timeformat

like image 57
Aaron Harker Avatar answered Nov 14 '22 22:11

Aaron Harker