![](/img/trans.png)
[英]Codeigniter count number of articles in one category while listing categories
[英]count posts one category in CodeIgniter
我在count上遇到了问题,并对其进行了分类。 我想在sidbar中显示类别,并根据帖子数对它们进行计数。
例如。
我的帖子表是“电子书”我的类别表是“类别”
我的模型是:
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>
我想在此处显示基于一个类别的帖子数:{HERE}
先感谢您
您可以尝试以下方法:
$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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.