【问题标题】:how can i create pagination from a single table using group_by and count_all_results Codigniter如何使用 groupby 和 count_all_results Codeigniter 从单个表创建分页
【发布时间】:2016-11-04 15:07:09
【问题描述】:

我有两张桌子 1.

order_log

order_id  order_status provider_id more
  1              2            2     ...
  2              2            1     ...
  3              0            1     ...
  4              3            2     ...
  5              1            3     ...
  6              1            1     ...
  7              4            2     ...
...

还有一张桌子

provider table
   provider_id   provider_name
    1               vodafone
    2                   xyz
    3                   abc
    4                   njk
....  

这是我从表中获取数据的代码

$this->load->model('ordermodel');
    $order_status=$this->ordermodel->getOrderStatus();


    $this->db->select($this->orderLogTable.'.operator_id,'.$this->orderLogTable.'.order_status,COUNT('.$this->db->dbprefix($this->orderLogTable).'.order_status) AS numofstatus, SUM('.$this->db->dbprefix($this->orderLogTable).'.order_customer_amount) AS totalamount,'.$this->operatorTable.'.operator_name, SUM('.$this->db->dbprefix($this->orderLogTable).'.order_retailer_discount_amount) AS totaldiscountamount');
    $this->db->from($this->operatorTable);
    $this->db->join($this->orderLogTable,$this->orderLogTable.'.operator_id ='.$this->operatorTable.'.operator_id AND '.'date(' . $this->db->dbprefix($this->orderLogTable) . '.order_complete_date) BETWEEN "' . date('Y-m-d', strtotime($fromdate)) . '" and"' . date('Y-m-d', strtotime($todate)) . '" AND '.$this->orderLogTable.'.order_status IN ('.$order_status['cancel'].','.$order_status['success'].','.$order_status['pending'].')','left');
    $this->db->group_by(array($this->orderLogTable.'.order_status',$this->operatorTable.'.operator_id'));
    $this->db->order_by($this->operatorTable.'.operator_id');
    if ($limit !== NULL && $offset !== NULL) {
        $this->db->limit($limit, $offset);
     }
    $query=$this->db->get();
    return $query->result_array();

完美的得到结果

但是当我试图获取分页数时

$this->load->model('ordermodel');
        $order_status=$this->ordermodel->getOrderStatus();
        $this->db->select($this->operatorTable.'.operator_id,'.$this->orderLogTable.'.order_status');
        $this->db->from($this->operatorTable);
        $this->db->join($this->orderLogTable,$this->orderLogTable.'.operator_id ='.$this->operatorTable.'.operator_id AND '.'date(' . $this->db->dbprefix($this->orderLogTable) . '.order_complete_date) BETWEEN "' . date('Y-m-d', strtotime($fromdate)) . '" and"' . date('Y-m-d', strtotime($todate)) . '" AND '.$this->orderLogTable.'.order_status IN ('.$order_status['cancel'].','.$order_status['success'].','.$order_status['pending'].')','left');
        //$this->db->group_by(array($this->orderLogTable.'.order_status',$this->operatorTable.'.operator_id'));
    /*here group by is off for count_all_results()*/
        //$query = $this->db->get();
        return $this->db->count_all_results();

它返回 1 当我使用 group by 它返回 3

但是有84行

我该如何解决这个问题? 请我不想使用 num_rows()

【问题讨论】:

  • 你明白我的问题是什么吗??我不是在质疑如何进行分页。我问的是如何在这个问题中为该分页计数。我只想使用 count_all_rows()
  • 请尝试了解我的情况,而不是检查我的问题是否重复

标签: count group-by codeigniter-3


【解决方案1】:
$this->load->model('ordermodel');

    //for count all records 
    $count_all = $this->db->count_all('your table');

    $order_status=$this->ordermodel->getOrderStatus();
    $this->db->select($this->operatorTable.'.operator_id,'.$this->orderLogTable.'.order_status');
    $this->db->from($this->operatorTable);
    $this->db->join($this->orderLogTable,$this->orderLogTable.'.operator_id ='.$this->operatorTable.'.operator_id AND '.'date(' . $this->db->dbprefix($this->orderLogTable) . '.order_complete_date) BETWEEN "' . date('Y-m-d', strtotime($fromdate)) . '" and"' . date('Y-m-d', strtotime($todate)) . '" AND '.$this->orderLogTable.'.order_status IN ('.$order_status['cancel'].','.$order_status['success'].','.$order_status['pending'].')','left');
    //$this->db->group_by(array($this->orderLogTable.'.order_status',$this->operatorTable.'.operator_id'));
/*here group by is off for count_all_results()*/
    //$query = $this->db->get();
    return $this->db->count_all_results();

您可以使用$this->db->count_all('your table'); 来统计所有记录。

【讨论】:

    猜你喜欢
    • 2012-12-21
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多