简体   繁体   English

PHP日历-从PHP获取事件

[英]PHP Calendar - Get events from PHP

I am using the FullCalendar plugin. 我正在使用FullCalendar插件。 I would like to show events saved in a DB table, is there a way to select and show these? 我想显示保存在数据库表中的事件,是否可以选择并显示这些事件?

Javascript: 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. 服务应包含使用PDO之类的查询以从数据库中获取数据。

I recommend starting here: https://phpdelusions.net/pdo 我建议从这里开始: 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". 在此示例中,关联数组具有一个名为“ data”的键,该键包含带有两个键“ name”和“ value”的关联数组的索引数组。

You then set the header to 'Content-Type: application/json'so that the browser knows how to handle the response. 然后,您将标头设置为“ Content-Type:应用程序/ json”,以便浏览器知道如何处理响应。

Finally you want process the nested array of data into JSON using the json_encode function. 最后,您想使用json_encode函数将嵌套的数据数组处理为JSON。

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: 假设它位于foo.com/get-events.php,则以下请求:

$.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: 将输出以下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 - 使用PDO获取/设置数据

- A simple service that handles the data from the DB - 处理来自数据库的数据的简单服务

- JSON response helper Class -JSON响应助手类

- DB/Table Info and setup - 数据库/表信息和设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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