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

如何在CodeIgniter视图中展示各分类的文章数量统计结果

CodeIgniter分类文章统计视图实现方案

问题背景

开发基于CodeIgniter的项目,需展示满足以下条件的分类文章统计:

  • 仅展示与POST表关联的分类
  • 分类的CATEGORY_TYPE为TYPE_POST
  • 需显示分类名称、该分类下文章总数、该分类文章占总文章数的百分比

现有代码

模型代码

class My_model extends CI_Model {

    function get_post_by_category() {
        $q = $this->db->select(' POST.CATEGORY_ID, CATEGORY.CATEGORY_NAME, COUNT(POST.CATEGORY_ID) as total_posts,  COUNT(POST.CATEGORY_ID)/COUNT(*) * 100 as percentage')
                      ->from('POST')
                      ->join('CATEGORY', 'POST.CATEGORY_ID= CATEGORY.ID', 'left')
                      ->where('CATEGORY.CATEGORY_TYPE', 'TYPE_POST')
                      ->group_by('POST.CATEGORY_ID, CATEGORY.CATEGORY_NAME') 
                      ->order_by('POST.CATEGORY_ID', 'ASC')
                      ->get();
        return $q->result();
    }
}

控制器代码

public function __construct() {
        parent::__construct();
        $this->load->model('My_model');
    }

    function index() {
        $this->load->model('My_model');
        // 加载模型方法获取统计数据
        $data['countdata']=$this->My_model->get_post_by_category(); 
        // 传递数据到视图
        $this->load->view('data/my_view', $data);
    }

原有视图代码(存在疑问部分)

<table border="1">  
      <tbody>  
         <tr>  
            <td> 分类名称</td>  
            <td> 分类下文章总数</td> 
            <td> 占比%</td>  
         </tr>  
         <?php  
         foreach ($countdata as $row1)  
         {  
            ?><tr>  
            <td><?php echo $row1->CATEGORY_NAME;?></td>  
            <td><?php echo $row1->total_posts;--WHAT TO DO HERE TO GET NUMBER OF POST FOR EACH CATEGORY--;?></td>  
            <td><?php echo $row1->percentage;<!--WHAT TO DO HERE TO GET THE PERCENTAGE OF ALL POST FOR ALL CATEGORIES-->%;?></td>  
            </tr>  
         <?php }  
         ?>  
      </tbody>  
   </table> 

修正方案

1. 模型代码修正(百分比计算错误修复)

原模型中百分比计算逻辑有误,COUNT(*)在分组查询后仅返回当前分组的记录数,无法得到全量文章总数。需先查询符合条件的总文章数,再计算每组占比:

class My_model extends CI_Model {

    function get_post_by_category() {
        // 先获取符合条件的总文章数
        $total_posts = $this->db->select('COUNT(*) as total')
                               ->from('POST')
                               ->join('CATEGORY', 'POST.CATEGORY_ID = CATEGORY.ID')
                               ->where('CATEGORY.CATEGORY_TYPE', 'TYPE_POST')
                               ->get()
                               ->row()->total;

        // 分组查询各分类文章数
        $q = $this->db->select('POST.CATEGORY_ID, CATEGORY.CATEGORY_NAME, COUNT(POST.CATEGORY_ID) as total_posts')
                      ->from('POST')
                      ->join('CATEGORY', 'POST.CATEGORY_ID = CATEGORY.ID', 'left')
                      ->where('CATEGORY.CATEGORY_TYPE', 'TYPE_POST')
                      ->group_by('POST.CATEGORY_ID, CATEGORY.CATEGORY_NAME') 
                      ->order_by('POST.CATEGORY_ID', 'ASC')
                      ->get();
        
        // 为每条数据添加占比字段
        $result = $q->result();
        foreach($result as $row){
            $row->percentage = $total_posts > 0 ? round(($row->total_posts / $total_posts) * 100, 2) : 0;
        }
        return $result;
    }
}

2. 视图代码修正(正确调用数据)

视图中只需直接调用模型返回的字段即可,移除多余注释,同时优化格式和安全性:

<table border="1">  
    <thead>
        <tr>  
            <th>分类名称</th>  
            <th>分类下文章总数</th> 
            <th>占比%</th>  
        </tr>  
    </thead>
    <tbody>  
        <?php foreach ($countdata as $row): ?>
        <tr>  
            <td><?php echo htmlspecialchars($row->CATEGORY_NAME); ?></td>  
            <td><?php echo $row->total_posts; ?></td>  
            <td><?php echo $row->percentage; ?>%</td>  
        </tr>  
        <?php endforeach; ?>
    </tbody>  
</table> 

说明

  • 模型中新增总文章数查询,确保百分比基于全量符合条件的文章计算
  • 视图中使用htmlspecialchars防止XSS注入,同时简化循环写法
  • 百分比保留两位小数,提升展示精度

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 17:20:36