Reputation: 509
I'm writing a web app, with Codeigniter, that allows a user to enter query parameters and then spits out the results.
For pagination, I'm passing a limit and offset (pretty standard stuff) and can return results based on that, however, I'm having trouble passing back the TOTAL number or records that would have been returned without using the LIMIT and OFFSET parameters.
My question: Is it possible to pass the total row count that would have been returned by a previous query using Codeigniters AR syntax?
I've tried a few variations of the below, but have (at best) been able to return a count of ALL records in the items table (using count_all_results). I feel like I'm missing something here.
if (isset($brand)) {$this->db->where('brand', $brand);}
if (isset($color)) {$this->db->where('color', $color);}
$items = $this->db->get('items', $page, $offset)->result_array();
$rowcount = $this->db->count_all_results('items);
return array('items' => $items, 'row_count' => $rowcount);
Upvotes: 0
Views: 993
Reputation: 12197
Yes,
if (isset($brand)) {$this->db->where('brand', $brand);}
if (isset($color)) {$this->db->where('color', $color);}
$query = $this->db->get('items', $page, $offset);
$items = $query->result_array();
$rowcount = $query->num_rows();
return array('items' => $items, 'row_count' => $rowcount);
Upvotes: 1