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

如何在CodeIgniter中向模型传递建筑代码参数

嘿,我来帮你搞定这个批量统计建筑工单的问题!你现在的问题是模型只能单个传参统计,要处理50个建筑肯定不能挨个硬编码,咱们可以从批量统计+视图对应展示的角度来优化,一步步来:

第一步:修改模型,实现批量统计所有建筑的工单数量

原来的getBldgCount只能处理单个建筑,咱们新增一个方法,用GROUP BY一次性统计所有建筑的工单总数,这样不用循环调用数据库,效率高很多:

public function getAllBldgCounts(){
    $this->db->select('Building_Code, COUNT(*) as total_requests');
    $this->db->from('work_requests');
    $this->db->group_by('Building_Code');
    $query = $this->db->get();
    
    // 把结果转成「建筑代码=>工单数量」的关联数组,方便视图快速取值
    $countMap = [];
    foreach($query->result_array() as $row){
        $countMap[$row['Building_Code']] = $row['total_requests'];
    }
    return $countMap;
}

// 如果还需要单个建筑的统计,原来的方法可以保留,它本身已经支持传参了
public function getBldgCount($queryBuilding){
    $this->db->select('*');
    $this->db->where('Building_Code', $queryBuilding);
    $this->db->from('work_requests');
    return $this->db->count_all_results();
}

另外,如果你还没有获取所有建筑列表的方法,也可以在模型里补充一个:

public function getAllBuildings(){
    $this->db->select('Building_Code');
    $this->db->from('buildings'); // 这里替换成你的建筑表名称
    return $this->db->get()->result_array();
}

第二步:修改控制器,传递所有建筑和对应的统计数据

控制器里要同时获取建筑列表和批量统计结果,一起传给视图:

// 获取所有建筑的列表
$data["buildings"] = $this->Buildings_model->getAllBuildings();
// 获取所有建筑的工单统计映射
$data["bldgCounts"] = $this->Buildings_model->getAllBldgCounts();
// 如果需要动态进度条,可以计算最大工单数(用于百分比计算)
$data["maxRequests"] = !empty($data["bldgCounts"]) ? max($data["bldgCounts"]) : 0;

$this->load->view('main/header');
$this->load->view('main/body', $data);
$this->load->view('main/footer');

第三步:修改视图,匹配每个建筑的统计数据

循环建筑列表时,从$bldgCounts数组里取出对应建筑的工单数量,替换原来固定的$countBuildings:

<div class="col-md-4 col-sm-4 col-xs-12">
 <div class="x_panel tile">
  <div class="x_title">
   <h2>Buildings</h2>
   <div class="clearfix"></div>
  </div>
  <div class="x_content">
   <h4>Total work requests</h4>
   <?php foreach ($buildings as $bldg){
       // 取出当前建筑的工单数量,没有工单的话显示0
       $currentCount = isset($bldgCounts[$bldg['Building_Code']]) ? $bldgCounts[$bldg['Building_Code']] : 0;
       // 计算进度条百分比(基于最大工单数)
       $progressPercent = $maxRequests > 0 ? round(($currentCount / $maxRequests) * 100) : 0;
   ?>
   <div class="widget_summary">
    <div class="w_left w_25">
     <span><?php echo $bldg['Building_Code'];?></span>
    </div>
    <div class="w_center w_55">
     <div class="progress">
      <div class="progress-bar bg-green" role="progressbar" 
           aria-valuenow="<?php echo $currentCount;?>" 
           aria-valuemin="0" 
           aria-valuemax="<?php echo $maxRequests;?>" 
           style="width: <?php echo $progressPercent;?>%;">
       <span class="sr-only"><?php echo $progressPercent;?>% Complete</span>
      </div>
     </div>
    </div>
    <div class="w_right w_20">
     <span><?php echo $currentCount; ?></span>
    </div>
    <div class="clearfix"></div>
   </div>
   <?php } ?>
  </div>
 </div>
</div>

这样改完之后,不管有多少个建筑,都能自动统计并展示对应的工单数量,再也不用逐个硬编码啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:06:52