<?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']; $event['status'] = $row['status']; $events[] = $event; } //返回JSON格式数据 echo json_encode($events); ?>修改代码块,使status值为pending的数据用灰色字体显示在PHP前端页面
时间: 2023-08-31 14:40:05 浏览: 83
你可以使用以下代码块来实现:
```php
<?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'];
//判断status值,如果为pending则设置字体颜色为灰色
if ($row['status'] == 'pending') {
$event['status'] = '<span style="color: gray;">' . $row['status'] . '</span>';
} else {
$event['status'] = $row['status'];
}
$events[] = $event;
}
//返回JSON格式数据
echo json_encode($events);
?>
```
在上述代码块中,我们在获取每个事件的status值时添加了一个条件语句,如果status为pending,则使用HTML的`<span>`标签将该值包装成灰色字体输出,否则直接输出原始值。这样就可以实现status为pending的数据用灰色字体显示在PHP前端页面了。
阅读全文