Welcome Guest, Not a member yet? Register   Sign In
How to create categories and associate them with posts
#5

In your model, create function that groups the results on 'slug_category' and counts how many records are in each category.
PHP Code:
public function count_per_category()
{
 
 $query $this->db
  
->select('slug_category, COUNT(id) as cat_count')
 
 ->from('posts')
 
 ->group_by('slug_category')
 
 ->order_by('slug_category')
 
 ->get();
 
 return 
$query->result_array();



Call this function from your controller, not from the view.
Controller:
PHP Code:
$data['cpcs'] = $this->admin_model->count_per_category();
$this->load->view('post/category',$data); 

In the view:
PHP Code:
<table>
<
tr><th>Category</th><th>Count</th></tr>
<?
php foreach($cpcs as $cpc) : ?>
  <tr>
    <td><?= $cpc['slug_category'];?></td>
    <td><?= $cpc['cat_count'];?></td>
  </tr>
<?php endforeach; ?>
</table> 
Reply


Messages In This Thread
RE: How to create categories and associate them with posts - by Wouter60 - 09-16-2017, 03:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB