Welcome Guest, Not a member yet? Register   Sign In
How to get correct Number of Comments (with 4 connected tables: Art./Cat./Users/Comm.)
#1

[eluser]sigork[/eluser]
4 standard connected tables:

1) Articles (Articles.categories; Articles.userid; Articles.articleid)
2) Categories (Categories.categoriid)
3) Users (Users.userid)
4) Comments (Comments.com_articleid)

Front page should show

1) Articles - info
2) Categories - info
3) Users - info
4) Comments - comments per article

My code is:

Code:
$this->db->limit(2); // 2 articles per page (pagination test)
$this->db->orderby('articleid', 'desc');

$this->db->select('*');
$this->db->select('Categories.categoryid', 'Categories.category_name');
$this->db->select('Users.userid', 'Users.username');
$this->db->select('com_articleid, COUNT(*) AS `comments_count`');

$this->db->group_by('articleid');

$this->db->from('Articles');

$this->db->join('Categories', 'Articles.categories = Categories.categoryid', 'left');
$this->db->join('Users', 'Articles.userid = Users.userid', 'left');
$this->db->join('Comments', 'articleid = com_articleid', 'left');

$data['query'] = $this->db->get();

Everything works but there is one (I hope) problem:
Quote:If an Article has 0 Comments, it shows 1 Comment (!)
If an Article has 1 Comments, it shows 1 Comment
...
If an Article has 9 Comments, it shows 9 Comment

For example,

- Articles.articleid= 7
- Comments.com_articleid has no 7 (No comments for this Article)
- Article shows it has 1 Comment.

View:

Code:
<?php foreach($query->result() as $row): ?>
...
<?=anchor ... $row->comments_count ... ;?>
...
<?php endforeach; ?>

Is there any obvious problem/error in my code, or how to debug this code?

Thanks!


Messages In This Thread
How to get correct Number of Comments (with 4 connected tables: Art./Cat./Users/Comm.) - by El Forum - 07-19-2009, 08:35 AM



Theme © iAndrew 2016 - Forum software by © MyBB