2016-11-04 47 views
-2

從單一的表中創建分頁我有兩個表 1.如何使用我和GROUP_BY count_all_results Codigniter

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

+0

http://stackoverflow.com/questions/33051222/how-to-paginate-with-codeigniter-3-0-1 –

+0

你明白我的問題是什麼?我不問如何分頁..我問如何計算在這個問題的分頁。我只是想使用count_all_rows() –

+0

請嘗試瞭解我的情況,而不是檢查我的問題是否重複 –

回答

0
$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');爲計數的所有記錄。