Welcome Guest, Not a member yet? Register   Sign In
Help with code bloat and SQL...and oh my just help :(
#2

[eluser]OES[/eluser]
You can achive your counts with SQL. Just look up mysql COUNT in google. And you would need GROUP also.

You can still use Active recored to get your results.

Here is an example I used to count comments per news ID.

Code:
function list_entries($limit, $cat='', $pag='', $app)
    {
        $this->db->select("ci_newsentries.*, ci_newscategories.newscategory_title AS catname, ci_newscategories.newscategory_title_sef AS caturl, COUNT(ci_newscomments.newscomment_id) AS no_comments");
        $this->db->from('ci_newsentries');
        $this->db->join('ci_newscomments', 'ci_newsentries.newsentry_id = ci_newscomments.newscomment_newsentry_id', 'left');
        $this->db->join('ci_newscategories', 'ci_newsentries.newsentry_category = ci_newscategories.newscategory_id', 'left');
        $this->db->where('ci_newsentries.newsentry_approved', $app);
        if ($cat):
            $this->db->where('ci_newsentries.newsentry_category', $cat);
        endif;
        $this->db->group_by("ci_newsentries.newsentry_id");
        $this->db->order_by("ci_newsentries.newsentry_date", "desc");
        $this->db->limit($limit, $pag);
        $query = $this->db->get();
        return $query->result_array();
    } // list_entries()

This should hopefully get you on your way.


Messages In This Thread
Help with code bloat and SQL...and oh my just help :( - by El Forum - 10-14-2008, 11:20 PM
Help with code bloat and SQL...and oh my just help :( - by El Forum - 10-16-2008, 04:47 AM



Theme © iAndrew 2016 - Forum software by © MyBB