[eluser]hyperfire[/eluser]
Hi there!
I have this doubt about counting the search results for ages, maybe its just better ask
Every time I have a page on CI, that uses pagination, as I use a lot of joins and parameters on my queries, I need to request the same query to the db twice, the first one to get the full result count, and the second with the limit and offset params so CI navigation works.
How do you guys do it in order to reduce the requests to db?
Here's a sample code:
Code:
// Query for all results (pagination total records)
$this->mdl_grouped_areas->events_fetch_from_cat = true;
$this->mdl_grouped_areas->events_activeornot= 1; // Get only active items
$this->mdl_grouped_areas->get_agenda(); // execute()
$content['total_records'] = $this->mdl_grouped_areas->events_num_records;
// Query paged
$this->mdl_grouped_areas->events_fetch_from_cat = true;
$this->mdl_grouped_areas->events_activeornot= 1; // Get only active items
$this->mdl_grouped_areas->events_pag_amount = $limit;
$this->mdl_grouped_areas->events_pag_offset = $offset;
$this->mdl_grouped_areas->events_sortby = 'events.dt_post DESC';
$content['pageJuice'] = $this->mdl_grouped_areas->get_agenda(); // execute()
// Pagination
$this->config_pag['base_url'] = base_url() . '/' . $this->area_c . '/featured/';
$this->config_pag['total_rows'] = $content['total_records'];
$this->config_pag['uri_segment'] = 3;
$this->pagination->initialize($this->config_pag);
In the example above, after execute the code for the first time, without offset and limit params, the model gets a variable with the full result count for that search.
On the second query, results are fetched with limits then pagination is initialized before calling the views.
Is there some clever way to get the same results? Am I doing something wrong?
Thanks!!!