codeigniter show how many threads in a category
-
09-02-2021 - |
Question
im working on a forum in codeigniter and i want to show how many Threds there is in a category, but my problem is that it only shows the categorys that allready have been posted in.
and i want to show ALL the category although there is no posts in it. how can i do that ?.
here is my model file
//Load the category list to the forum frontpage
function loadCategoryList() {
$this->db->select('forumCategory.id as categoryID, category, description, COUNT(forumThread.id) as threadID');
$this->db->where('forumCategory.approved', 'yes');
$this->db->join('forumThread', 'forumThread.fk_forumCategory = forumCategory.id');
$this->db->group_by('categoryID');
$loadCategory = $this->db->get('forumCategory');
return $loadCategory->result();
}
Solution
When you join forumThread, you are eliminating records in your result set that don't have threads. Left join it instead to leave those in.
$this->db->join('forumThread', 'forumThread.fk_forumCategory = forumCategory.id', 'left');
More details here: http://codeigniter.com/user_guide/database/active_record.html
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow