You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中根据MySQL查询的预约状态值动态修改对应字体颜色

实现方案

前提说明

确认你查询$resultlist的SQL语句已经包含预约状态字段,默认该字段名为appointment_status,如果实际字段名不同请自行替换代码中对应内容。

修改步骤

步骤1:新增表头状态列

在的中新增状态列表头,示例放在「case」列之后:

<thead>
    <tr>
        <th><?php echo $this->lang->line('appointment') . " " . $this->lang->line('date'); ?></th>
        <!-- <th><?php echo $this->lang->line('opd') . " " . $this->lang->line('id'); ?></th>  -->
        <th><?php echo $this->lang->line('patient') . " " . $this->lang->line('name'); ?></th>
        <th><?php echo $this->lang->line('mobile_no'); ?></th>
        <th><?php echo $this->lang->line('casualty'); ?></th>
        <th><?php echo $this->lang->line('consultant') . " " . $this->lang->line('doctor'); ?></th>
        <th><?php echo $this->lang->line('case'); ?></th>
        <!-- 新增预约状态表头 -->
        <th><?php echo $this->lang->line('appointment_status') ?? '预约状态'; ?></th>
        <th><?php echo $this->lang->line('generated_by'); ?></th>
        <th><?php echo $this->lang->line('generted_time'); ?></th> 
    </tr>
</thead>

步骤2:循环内新增颜色判断逻辑

在foreach ($resultlist as $report)循环内部,处理完$paymenttype判断后新增状态样式逻辑:

PHP 8.0+ 版本写法(推荐)

// 预约状态颜色判断
$status = $report['appointment_status'];
$statusClass = match($status) {
    'Pending' => 'text-warning', // 黄色,对应Bootstrap样式类
    'Approved' => 'text-success', // 绿色
    'Reject' => 'text-danger', // 红色
    default => 'text-muted'
};

PHP 8.0以下兼容写法

// 预约状态颜色判断
$status = $report['appointment_status'];
$statusClass = 'text-muted';
if ($status == 'Pending') {
    $statusClass = 'text-warning';
} elseif ($status == 'Approved') {
    $statusClass = 'text-success';
} elseif ($status == 'Reject') {
    $statusClass = 'text-danger';
}

注:如果你的项目没有使用Bootstrap,把$statusClass的值替换为内联样式即可,比如$statusClass = 'color: #ffc107;',后续输出时绑定到style属性即可。

步骤3:表格行新增状态列输出

在tbody的对应位置新增状态列,绑定样式:

<tr>
    <td><?php echo date($this->customlib->getSchoolDateFormat(true), strtotime($report['appointment_date'])) ?></td>
    <!-- <td><?php echo $report['id']; ?></td> -->
    <td>
        <a href="<?php echo base_url(); ?>admin/patient/profile/<?php echo $report['pid']; ?>"><?php echo $report['patient_name'] ?>
        </a>
    </td>
    <td><?php echo $report['mobileno']; ?></td>
    <td><?php echo date('h:i a ', strtotime($report['casualty'])); ?></td>
    <td><?php echo $report['name']." ".$report['surname']; ?></td>
    <td><?php echo $report['case_type'];?></td>
    <!-- 新增状态列 -->
    <td class="<?php echo $statusClass; ?>"><?php echo $status; ?></td>
    <?php
        $this->db->select('name');
        $this->db->from('staff');
        $this->db->where('id', $report['generated_by']);
        $query = $this->db->get();
        $generated_by = $query->row();
    ?>
    <td><?php echo $generated_by->name; ?></td>
    <td><?php echo $report['created_at'] ; ?></td>
</tr>

其他说明

如果你不需要单独新增列,想把状态显示在现有列中,直接把状态和样式写到对应列的输出位置即可。另外原代码末尾多余了一个</tr>标签,建议删除避免HTML结构错误。

内容的提问来源于stack exchange,提问作者Hossam Shaker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 06:54:05