【问题标题】:count posts one category in CodeIgniter在 CodeIgniter 中统计一个类别的帖子
【发布时间】:2018-09-28 12:34:24
【问题描述】:

我在统计帖子类别并显示它们时遇到问题。 我想在 sidbar 中显示类别并根据帖子对它们进行计数。

例如。

  • num=有医疗的帖子数为 8
  • 医疗=8

我的帖子表是“电子书” 我的类别表是“类别”

我的模型是:

    public function count_item(){
    $this->db->select('*');
    $this->db->from('categories');
    $this->db->join('ebooks', 'categories.cat_id = ebooks.cat_id');
    $this->db->where(array('ebooks.cat_id !=' => null, 'categories.cat_id != ' => NULL));
    $this->db->group_by(array('categories.cat_id', 'categories.name'));
    $query = $this->db->get();
    return $query->result_array();
}

我的控制器是:

  public function posts() {

    $data['posts'] = $this->post_model->get_posts();
    $data['categories'] = $this->categories_model->count_item();

    $this->load->view('inc/header');
    $this->load->view('posts/posts', $data);
    $this->load->view('inc/footer');

}

我的看法是:

 <div class="panel-body">
                    <?php foreach ($categories as $category): ?>
                    <ul class="list-group">
                        <li class="list-group-item"><a href="#"><?php 
                            echo $category['name']; 
                            ?><span class="badge" style="float: left">14</span></a></li>
                    </ul>
                    <?php endforeach; ?>
                    <div class="clearfix"></div>
                    <center><a href="" class="btn btn-primary">more details</a></center>
                </div>

我想在此处显示基于一个类别的帖子数量: {这里}

提前谢谢你

【问题讨论】:

    标签: php sql codeigniter-3


    【解决方案1】:

    你可以试试这个:

    $this->db->select('categories.name as categories_name, COUNT(ebooks.cat_id) as categories_count');
    $this->db->from('categories');
    $this->db->join('ebooks', 'categories.cat_id = ebooks.cat_id');
    $this->db->where(array('ebooks.cat_id !=' => null, 'categories.cat_id != ' => NULL));
    $this->db->group_by('ebooks.cat_id');
    $query = $this->db->get();
    return $query->result_array();
    

    【讨论】:

      猜你喜欢
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多