简体   繁体   中英

PHP Calendar - Get events from PHP

I am using the FullCalendar plugin. I would like to show events saved in a DB table, is there a way to select and show these?

Javascript:

$(document).ready(function() {
    $('#calendar').fullCalendar({
        defaultDate: '2016-09-12',
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        events: [
            {
                id: 999,
                title: 'Test Event',
                start: '2016-09-09',
                end: '2016-09-11'
            }

There are quite a few thing you will need to do, the approach below is really only for a small one-off project, it will not scale very well. It will outline the basic steps you will need to take.

You need to write a simple service that accepts a query string param of the selected date.

The service should contain a query using something like PDO to fetch the data from the database.

I recommend starting here: https://phpdelusions.net/pdo

Essential you can use a prepared statement to safely use the date value as selection criteria.

You would then structure your response using arrays. In this example an associative array has one key named "data" that contains an indexed array of associative arrays with two keys "name" and "value".

You then set the header to 'Content-Type: application/json'so that the browser knows how to handle the response.

Finally you want process the nested array of data into JSON using the json_encode function.

The most bare bones service would look like this:

<?php
date_default_timezone_set('UTC');//Set time zone appropriately
//Get date from url , I set a default if there is no date, you mat want to throw an error...

//Error throw if data param missing!
$myDate = $_GET['date'];

//Query DB to get back a result set. The sample data below is allows you to get a JSON respons back.
//check out this tutorial to get a good understanding of how to use PDO to get your data from the DB: https://phpdelusions.net/pdo

//This represents the data from your query.
$foo = Array(
    data => Array(
        Array(
            'name' => 'Bar',
            'value' => 42,
            ),
            Array(
            'name' => 'Baz',
            'value' => -42,
            )
        )
);

//Make sure you set the header and then echo the content, there should be no extra white space after the header is set!
header('Content-Type: application/json');
echo json_encode($foo);
?>

Assuming this was located at foo.com/get-events.php, the following request:

$.ajax({
    url: 'http://foo.com/get-events.php',
    method: 'GET',
    dataType: 'json'
})
.done(function(data) {
//Use data to set calendar!!!
console.log('Data: ', data);
});

Would output the following JSON:

{"data":[{"name":"Bar","value":42},{"name":"Baz","value":-42}]}

Also here is a simple structured project that promotes code reuse and covers the topics outlined above. Good parts of it you may be able to copy/paste and change to meet your needs

- Getting/Setting data using PDO

- A simple service that handles the data from the DB

- JSON response helper Class

- DB/Table Info and setup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM