[英]PHP Calendar - Get events from PHP
我正在使用FullCalendar插件。 我想显示保存在数据库表中的事件,是否可以选择并显示这些事件?
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'
}
您将需要做很多事情,以下方法实际上仅适用于小型一次性项目,无法很好地扩展。 它将概述您需要采取的基本步骤。
您需要编写一个简单的服务,该服务接受所选日期的查询字符串参数。
服务应包含使用PDO之类的查询以从数据库中获取数据。
我建议从这里开始: https : //phpdelusions.net/pdo
必不可少的是,您可以使用准备好的语句来安全地使用日期值作为选择标准。
然后,您将使用数组来构造响应。 在此示例中,关联数组具有一个名为“ data”的键,该键包含带有两个键“ name”和“ value”的关联数组的索引数组。
然后,您将标头设置为“ Content-Type:应用程序/ json”,以便浏览器知道如何处理响应。
最后,您想使用json_encode函数将嵌套的数据数组处理为JSON。
最裸露的服务看起来像这样:
<?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);
?>
假设它位于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);
});
将输出以下JSON:
{"data":[{"name":"Bar","value":42},{"name":"Baz","value":-42}]}
这也是一个简单的结构化项目,可促进代码重用并涵盖上面概述的主题。 您可以复制/粘贴和更改其中的大部分内容以满足您的需求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.