CodeIgniter Forums
New to CI and need help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: New to CI and need help (/thread-71312.html)



New to CI and need help - BusyWitch - 07-31-2018

Hi... I am brand new to CI and I've been going through the "news" tutorial. I am adding onto it with "articles" (copy of the "news" section) and I wanted to have a list of article categories. Everything is going great, but when I got to my list of categories... well, I would like to print out how many articles in each category, but I have something wrong and I'm not sure what.

<code>
<ul>
<?php
$this->db->select('*, count(*)');
$this->db->from('article_cats');
$this->db->join('articles', 'articles.cat_id = article_cats.cat_id');
$this->db->group_by('article_cats.cat_id');
$query = $this->db->get();
$articlecount = $query->num_rows();
foreach ($query->result() as $row) {
print '<li>'.$articlecount.'&nbsp; <a href="'.ARTICLES.'/'.$row->cat_slug.'" title="'.$row->cat_title.'">'.$row->cat_title.'</a></li>';
}
?>
</ul>
</code>

and this returns:
  • 3  Misc Articles
  • 3  Acne Articles
  • 3  Allergy Articles
"3" being all 3 articles currently in the database

Any and all help is greatly appreciated! Thanks!


RE: New to CI and need help - donpwinston - 07-31-2018

use count(*) as article_count, then output $row->article_count

query->num_rows is the total number of rows in your query not the article count.


RE: New to CI and need help - BusyWitch - 07-31-2018

(07-31-2018, 10:44 AM)donpwinston Wrote: use count(*) as article_count, then output $row->article_count

query->num_rows is the total number of rows in your query not the article count.

Thank you so very much!