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

CodeIgniter中如何将卡车航程频率查询结果按个性化数据表呈现

CodeIgniter 卡车航程频率统计表视图实现

一、控制器层数据传值逻辑

遵循CodeIgniter的MVC分层规范,在统计页面对应的控制器方法中,加载已编写的统计模型,依次调用三个模型方法获取统计数据,将数据打包传入指定视图文件:

// 控制器中对应页面的方法示例
public function frequence_voyage()
{
    // 加载自定义统计模型
    $this->load->model('Voyage_stats_model');
    // 依次获取模型返回的统计数据
    $data['voyage_freq_data'] = $this->Voyage_stats_model->display_frequenceVoyageTrajet();
    $data['camion_list'] = $this->Voyage_stats_model->display_frequenceVoyageTrajet1();
    $data['camion_total_count'] = $this->Voyage_stats_model->display_frequenceVoyageTrajet2()[0]->total;
    // 加载对应视图,传入数据
    $this->load->view('voyage/frequency_table', $data);
}

二、视图层数据预处理

数据库返回的是逐行的扁平统计结果,无法直接渲染为列维度随时间动态变化的交叉统计表,需要先在视图入口做数据维度映射:

  • 收集去重后的「年份+月份名称」作为动态表头列
  • 收集去重后的「航线+卡车牌号」作为固定行维度
  • 建立「行标识+表头标识」到出行频率值的映射关系,无匹配数据时默认填充0
// 视图入口处的数据预处理
$table_headers = []; // 存储动态表头
$table_rows = []; // 存储行维度
$freq_map = []; // 存储单元格值映射

foreach ($voyage_freq_data as $item) {
    // 拼接唯一表头标识:年份+月份名
    $header_key = $item->Year . ' ' . $item->monthname;
    // 去重收集所有表头
    if (!in_array($header_key, $table_headers)) {
        $table_headers[] = $header_key;
    }
    // 拼接唯一行标识:航线-卡车牌号
    $row_key = $item->trajet_Voyage . ' | ' . $item->imm_Camion;
    // 去重收集所有行
    if (!in_array($row_key, $table_rows)) {
        $table_rows[] = $row_key;
    }
    // 填充单元格映射值
    $freq_map[$row_key . '_' . $header_key] = $item->frequence;
}

三、个性化数据表渲染

按照目标统计表的结构,前两列固定为航线、卡车牌号,后续列依次展示动态生成的年月维度,配合自定义样式实现统一的视觉效果:

<!-- 自定义表格样式,可根据需求调整配色、间距 -->
<style>
.freq-table {
    width: 100%;
    border-collapse: collapse;
    margin: 2rem 0;
    font-size: 0.9rem;
}
.freq-table th {
    background-color: #f5f7fa;
    padding: 0.8rem 0.5rem;
    border: 1px solid #e4e7ed;
    font-weight: 600;
}
.freq-table td {
    padding: 0.7rem 0.5rem;
    border: 1px solid #e4e7ed;
    text-align: center;
}
.freq-table tr:hover {
    background-color: #f5f7fa;
}
.stats-bar {
    padding: 1rem;
    background: #ecf5ff;
    border-radius: 4px;
    margin-bottom: 1rem;
}
</style>

<!-- 统计概览条 -->
<div class="stats-bar">
    当前统计覆盖卡车总数:<strong><?= htmlspecialchars($camion_total_count) ?></strong> 台
</div>

<!-- 统计表主体 -->
<table class="freq-table">
    <thead>
        <tr>
            <th>航程航线</th>
            <th>卡车牌号</th>
            <!-- 循环渲染动态年月表头 -->
            <?php foreach ($table_headers as $header): ?>
                <th><?= htmlspecialchars($header) ?></th>
            <?php endforeach; ?>
        </tr>
    </thead>
    <tbody>
        <!-- 循环渲染每一行统计数据 -->
        <?php foreach ($table_rows as $row): 
            list($trajet, $camion) = explode(' | ', $row);
        ?>
        <tr>
            <td><?= htmlspecialchars($trajet) ?></td>
            <td><?= htmlspecialchars($camion) ?></td>
            <!-- 循环填充每个单元格的频率值 -->
            <?php foreach ($table_headers as $header): 
                $cell_key = $row . '_' . $header;
                $freq_value = $freq_map[$cell_key] ?? 0;
            ?>
                <td><?= $freq_value ?></td>
            <?php endforeach; ?>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

优化提示

  • 修正原模型SQL中的法语拼写错误:九月的Steptembre应调整为Septembre,避免月份名称展示异常
  • 调用display_frequenceVoyageTrajet1获取的全量卡车列表,可以补全统计行维度,确保没有出行记录的卡车也会在表格中展示,值默认填充为0,避免统计维度缺失
  • 可在表格末尾增加合计列,计算每个航线/卡车的年度总出行次数,提升统计信息的完整性
  • 可对高频出行的单元格添加背景色高亮,实现基础的数据可视化效果

内容的提问来源于stack exchange,提问作者Kara Bou

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.25 10:15:03