如何计算在项目组与codeigniter? [英] How to count items in group with Codeigniter?

查看:170
本文介绍了如何计算在项目组与codeigniter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的活动记录返回日期有以下code的分组列表:

I am using active record to return a grouped list of dates with the following code:

function get_archive_links(){

        $this->db->order_by('date','desc');
        $this->db->group_by('MONTH(date), YEAR(date)');
        $query = $this->db->get('blog'); 

    foreach ($query->result() as $row) {
        $data[] = array(
            'id' => $row->id,
            'date' => $row->date
        );
    }

    return $data;
}

其中环和回荡在我的网页返回时:

Which when looped and echoed on my page returns:

  • Febuary 2012
  • 在2012年1月
  • 在2012年12月

我怎样才能在组中计数的每个项目,并在我的列表中显示了吗?例如:

How can I count each item in the group and display it in my list? Example:

  • 在Febuary 2012(2)
  • 在2012年1月(6)
  • 在2012年12月(7)

我会指望有多少博客帖子在那里有每个月。

I will be counting how many blog posts there where for each month.

推荐答案

您需要可以删除 GROUP BY 再算上在PHP中,或添加 - >选择(COUNT(ID)AS num_posts')您的AR查询。我可能会添加 - >选择(),因为它是较少的工作:)

You need to either remove the GROUP BY then count in PHP, or add a ->select('COUNT(id) AS num_posts') to your AR query. I'd probably add the ->select(), as it's less work :)

这篇关于如何计算在项目组与codeigniter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆