我有一个庞大的数据库和很多事件。每次我进入“日历主页”(每月视图)时,都会加载数据库中的所有现有事件,尽管我只查看当前月份。目前,我的html页面超过3MB,并且倾向于减慢我的浏览器标签。
为了解决这个问题,我开始更改代码以仅获取当月的事件作为json。不幸的是,日期范围的开始和结束日期不起作用-该页面仍在从数据库中获取所有事件。我已经进行了几个小时的研究和许多调整。
到目前为止,我设法获取了FullCalendar.io事件-我有两个事件源-因此我使用了:
eventSources: [
{
url: 'include/load-calendar-event.php', // use the `url` property
color: '#008000',
},
{
url: 'include/load-calendar-event-retour.php', // use the `url` property
color: '#008000',
},
// any other sources...
],
用于获取事件的相应两个文件几乎相同-因此一个文件应该足够:
load-calendar-event.php
require_once('bdd.php');
$sql = "SELECT * FROM messages"; // this selects all rows
$req = $bdd->prepare($sql);
$req->execute();
$events = $req->fetchAll();
$data = array();
foreach($events as $event) {
$start = explode(" ", $event['start']);
$end = explode(" ", $event['end']);
if($start[1] == '00:00:00'){
$start = $start[0];
}else{
$start = $event['start'];
}
if($end[1] == '00:00:00'){
$end = $end[0];
}else{
$end = $event['end'];
}
$data[] = array(
'id'=> $event['id'],
'title'=> $event['title'],
'start'=> $start,
'end'=> $end,
'color'=> $event['color']
);
}
echo json_encode($data);
通过调用日历页面,浏览器将进行以下调用-请注意日期(2020-01-01-2020-02-01)和数据格式:
...include/load-calendar-event.php?start=2020-01-01&end=2020-02-01&_=1578601056565
和
...include/load-calendar-event-retour.php?start=2020-01-01&end=2020-02-01&_=1578601056566
事件的日期格式以以下格式(YYYY-MM-DD HH:mm:ss)保存在数据库中:
2020-01-11 10:00:00
题:
知道如何仅获取当月的事件吗?
为什么要选择表中的所有行并对其进行遍历?
您需要更新查询以获取作为$_GET
参数传递的开始和结束值。
require_once('bdd.php');
$sdate = $_GET['start']; //LIKE THIS
$edate = $_GET['end']; //AND THIS
$sql = "SELECT * FROM messages WHERE date >= ".$sdate." AND date <= ".$edate; // this selects all rows. Change 'date' to whatever your column name is in the database.
$req = $bdd->prepare($sql);
$req->execute();
$events = $req->fetchAll();
$data = array();
foreach($events as $event) {
$start = explode(" ", $event['start']);
$end = explode(" ", $event['end']);
if($start[1] == '00:00:00'){
$start = $start[0];
}else{
$start = $event['start'];
}
if($end[1] == '00:00:00'){
$end = $end[0];
}else{
$end = $event['end'];
}
$data[] = array(
'id'=> $event['id'],
'title'=> $event['title'],
'start'=> $start,
'end'=> $end,
'color'=> $event['color']
);
}
echo json_encode($data);
您只需使用PHP的date函数生成开始日期和结束日期,而不是使用查询字符串参数传入变量,就可以更轻松地做到这一点。尽管这将为您提供更大的灵活性。如果设置了这些值,您甚至可以包含一些if / else语句来生成不同的查询。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句