<?php //连接数据库 $mysqli = new mysqli("localhost", "root", "root123456", "hrms"); if ($mysqli->connect_errno) { die("连接失败: " . $mysqli->connect_error); } //查询日程 $result = $mysqli->query("SELECT * FROM event"); $events = array(); while ($row = $result->fetch_assoc()) { $event = array(); $event['id'] = $row['id']; $event['title'] = $row['title']; $event['start'] = $row['start']; $event['end'] = $row['end']; $events[] = $event; } //返回JSON格式数据 echo json_encode($events); ?><?php //连接数据库 $mysqli = new mysqli("localhost", "root", "root123456", "hrms"); if ($mysqli->connect_errno) { die("连接失败: " . $mysqli->connect_error); } //查询日程 $result = $mysqli->query("SELECT * FROM event"); $events = array(); while ($row = $result->fetch_assoc()) { $event = array(); $event['id'] = $row['id']; $event['title'] = $row['title']; $event['start'] = $row['start']; $event['end'] = $row['end']; $events[] = $event; } //返回JSON格式数据 echo json_encode($events); ?>修改代码块,使day试图下的日程显示在对应的时间栏里
时间: 2023-08-31 11:43:28 浏览: 203
可以通过修改循环中构造 $event 数组的代码,在其中加入判断逻辑,根据 $row['start'] 和 $row['end'] 的值,确定该日程的开始时间和结束时间,进而确定该日程应该显示在哪个时间栏里。以下是一个可能的修改方案:
```php
//查询日程
$result = $mysqli->query("SELECT * FROM event");
$events = array();
while ($row = $result->fetch_assoc()) {
$event = array();
$event['id'] = $row['id'];
$event['title'] = $row['title'];
$event['start'] = $row['start'];
$event['end'] = $row['end'];
//根据开始时间和结束时间确定时间栏的索引
$start_time = strtotime($row['start']);
$end_time = strtotime($row['end']);
$start_hour = date('G', $start_time);
$end_hour = date('G', $end_time);
$col_index = $start_hour - 8; //假设日程表的起始时间是8点
if ($start_hour == $end_hour) {
//日程开始和结束时间在同一时间栏
$event['start'] = date('Y-m-d H:i:s', mktime(8, $start_hour * 30, 0, date('n', $start_time), date('j', $start_time), date('Y', $start_time)));
$event['end'] = date('Y-m-d H:i:s', mktime(8, $end_hour * 30 + 30, 0, date('n', $end_time), date('j', $end_time), date('Y', $end_time)));
} else {
//日程跨越多个时间栏
$event['start'] = date('Y-m-d H:i:s', mktime(8, $start_hour * 30, 0, date('n', $start_time), date('j', $start_time), date('Y', $start_time)));
$event['end'] = date('Y-m-d H:i:s', mktime(8, $end_hour * 30, 0, date('n', $end_time), date('j', $end_time), date('Y', $end_time)));
}
$events[$col_index][] = $event;
}
//返回JSON格式数据
echo json_encode($events);
```
这个方案假设日程表从早上8点开始,每个时间栏表示30分钟,因此日程开始时的时间栏索引需要减去8。如果日程开始和结束时间在同一个时间栏内,直接使用 $row['start'] 和 $row['end'] 的值即可;如果日程跨越多个时间栏,需要对 $row['start'] 和 $row['end'] 的时间戳进行处理,构造新的开始和结束时间。最后,将每个日程添加到对应的时间栏中。
阅读全文