CodeIgniter Forums
Retrieving comments via active record? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Retrieving comments via active record? (/showthread.php?tid=48215)



Retrieving comments via active record? - El Forum - 01-08-2012

[eluser]Unknown[/eluser]
Hi,

How would you do a query that would join up and retrieve the comments for a news/blogpost
with the Active Record provided in CI?

I've two tables, lets called them "news" and "news_comments". I am currently showing
all news on a page and for each news i'd like to add a link that has the count for how
many comments that particular news has.

Example: Comments(5)

And so i would somehow have to join up the two tables and do a count against the news_comments table and store that count_value in my query. This is the simple query
i have so far. Note that it is a query displaying multiple news, it is not a "retrieve a single news item by id and then its comments", but rather lets get all the news and the commentCount for each news.

Code:
public function get_news_limited($startRow, $perPage)
{
  $this->db->order_by("posted", "desc");
  
  
  $query = $this->db->get("news",$perPage,$startRow);
  if($query->num_rows() > 0)
  {
   return $query->result_array();
  }
  return false;
}

Maybe someone is good at sql and active record and could figure this out.
Thanks