Codeigniter Active Record - Count Total Found Rows with Limit (MySQL)

前端 未结 2 569
谎友^
谎友^ 2021-02-10 16:20

I\'m using a complex SQL query in a Codeigniter model with a limit applied. I\'d like to count the total number of rows that would have been found if the limit and offset had no

2条回答
  •  长情又很酷
    2021-02-10 17:13

    One way I can think is to have variable $count when you call your function, and it will go something like this:

    function your_function($count = FALSE) {

    $this->db->select(...)
    
    //before limit you do this:
    
    if($count != FALSE):
    
    return $this->db->get->result_array()
    
    else :
    
    return $this->db->limit($limit, $offset)->get()->result_array();
    
    endif;
    }
    

    This way you can call function two time - one for count and the other for limit query:

    $count = count($this->your_function(TRUE));
    $data['query] = $this->your_function();
    

提交回复
热议问题