如何使用 Codeigniter 对组中的项目进行计数?

发布于 2025-01-08 19:26:33 字数 675 浏览 3 评论 0原文

我正在使用活动记录返回日期分组列表,代码如下:

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;
}

在我的页面上循环和回显时返回:

  • 2012 年 2 月
  • 2012 年 1 月
  • 2012 年 12 月

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

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

我将计算每个月有多少博客文章。

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
  • January 2012
  • December 2012

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

  • Febuary 2012 (2)
  • January 2012 (6)
  • December 2012 (7)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦中楼上月下 2025-01-15 19:26:33

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

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

肩上的翅膀 2025-01-15 19:26:33

你可以用sql count函数来实现。如果您使用分组依据,则计数函数返回行组的计数行。

像这样,

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;

}

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

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;

}

傲影 2025-01-15 19:26:33

这很完美,但如果不存在类别帖子,则不会显示零计数,

这是我的功能,

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');

}

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');

}
月棠 2025-01-15 19:26:33
SELECT
  categories.*,
  COUNT(posts.category_id) AS `count`
FROM
  (categories
LEFT JOIN
    posts 
ON posts.category_id = categories.category_id)
GROUP BY
  Category_name;

我已经找到了零计数问题的答案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文