如何使用Codeigniter计算组中的项目?

时间:2022-01-17 15:22:28

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年2月
  • January 2012
  • 2012年1月
  • December 2012
  • 2012年12月

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

如何计算组中的每个项目并将其显示在列表中?例:

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

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

我将计算每个月有多少博客帖子。

4 个解决方案

#1


2  

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 :)

您需要删除GROUP BY然后在PHP中计数,或者在AR查询中添加 - > select('COUNT(id)AS num_posts')。我可能会添加 - > select(),因为它的工作量较少:)

#2


1  

You can it with sql count function. if you are using group by, count function returning count rows for row group.

你可以使用sql count函数。如果您正在使用group by,则count函数返回行组的计数行。

like this,

喜欢这个,

function get_archive_links(){
    $this->db->select("count(*) as count, blog.*);
    $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,
            'count' => $row->count
        );
    }

return $data;

}

}

#3


0  

This works perfect but not shows zero counts if not exist post for category

这种方法很完美,但如果不存在类别,则不显示零计数

here is my function,

这是我的功能,

function get_category_list()
{
    $this->db->select("count(*) as num_posts,cb_category.*");
    $this->db->where_in('cb_post.city', $city_ids);
    $this->db->join('cb_posts', 'cb_post.category = cb_category.cat_id or cb_post.parent = cb_category.cat_id', 'left');

    $this->db->order_by('cat_name_en', 'ASC');

    $this->db->group_by('cb_category.cat_id');

    $query = $this->db->get('cb_category');

}

#4


0  

SELECT
  categories.*,
  COUNT(posts.category_id) AS `count`
FROM
  (categories
LEFT JOIN
    posts 
ON posts.category_id = categories.category_id)
GROUP BY
  Category_name;

I've got the answer for the zero count issue.

我得到零计数问题的答案。

#1


2  

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 :)

您需要删除GROUP BY然后在PHP中计数,或者在AR查询中添加 - > select('COUNT(id)AS num_posts')。我可能会添加 - > select(),因为它的工作量较少:)

#2


1  

You can it with sql count function. if you are using group by, count function returning count rows for row group.

你可以使用sql count函数。如果您正在使用group by,则count函数返回行组的计数行。

like this,

喜欢这个,

function get_archive_links(){
    $this->db->select("count(*) as count, blog.*);
    $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,
            'count' => $row->count
        );
    }

return $data;

}

}

#3


0  

This works perfect but not shows zero counts if not exist post for category

这种方法很完美,但如果不存在类别,则不显示零计数

here is my function,

这是我的功能,

function get_category_list()
{
    $this->db->select("count(*) as num_posts,cb_category.*");
    $this->db->where_in('cb_post.city', $city_ids);
    $this->db->join('cb_posts', 'cb_post.category = cb_category.cat_id or cb_post.parent = cb_category.cat_id', 'left');

    $this->db->order_by('cat_name_en', 'ASC');

    $this->db->group_by('cb_category.cat_id');

    $query = $this->db->get('cb_category');

}

#4


0  

SELECT
  categories.*,
  COUNT(posts.category_id) AS `count`
FROM
  (categories
LEFT JOIN
    posts 
ON posts.category_id = categories.category_id)
GROUP BY
  Category_name;

I've got the answer for the zero count issue.

我得到零计数问题的答案。