Reputation: 81
I am doing a complex query of a large database in my model and need to count the total rows before limiting the results for paging.
My model looks as follow:
public function get_categoryads($limit, $start, $cid, $type)
{
$this->db->start_cache();
// All your conditions without limit
$this->db->from();
$this->db->where(); // and etc...
$this->db->stop_cache();
$total_rows = $this->db->count_all_results(); // This will get the real total rows
// Limit the rows now so to return per page result
$this->db->limit($per_page, $offset);
$result = $this->db->get();
return array(
'total_rows' => $total_rows,
'result' => $result,
);
}
I don't understand how to now call it in my controller. 1. Get the results to be handed to the view 2. Get total_rows to be handed to the pagination config in the controller. How do I call the array of results & total_rows in my controller?
In my controller:
// call the model function to get the result List data if not in an array
$data['results'] = $this->my_model->get_categoryads(10, 0, $cid, $type);
Sure this wont work as the model pushes out an array... What should I then do in my controller to get the following:
// call the model function to get the result List data if not in an array
$data['results'] = $this->my_model->get_categoryads(10, 0, $cid, $type);
// Get total results for Pagination Config:
$config['total_rows'] = $this->db->count_all_results();
Do I do
$config['total_rows'] = $this->db->count_all_results($data['results']);
Upvotes: 0
Views: 1624
Reputation: 5891
I think you could do this in a better code way.
With that said:
public function get_categoryads($limit, $start, $cid, $type)
{
$this->db->select('COUNT(field) AS `total`')
->from('table');
$totalRows = $this->db->get()->row()->total;
$this->db->select('...')
->from('...')
->where('...')
->limit('...');
$totalRowsAfterLimit = $this->db->get()->num_rows();
$results = $this->db->get()->results();
return array('totalRows' => $totalRows,
'totalRowsAfterLimit' => $totalRowsAfterLimit,
'results' => $results);
}
Finally, in your controller you may do something like:
list($totalRows, $totalRowsAfterLimit, $results) = $this->my_model->get_categoryads('...');
And you are now able to loop through the results.
foreach($results as $result)
{
echo $result->some_field_from_database;
}
Upvotes: 0
Reputation: 647
You can do like this in controller:
$results = $this->my_model->get_categoryads(10, 0, $cid, $type);
// Array of result rows to be sent to view
$data['results'] = $results['result'];
// For pagination config:
$config['total_rows'] = $results['total_rows'];
Upvotes: 2
Reputation: 412
you can use codeigniter pagination concept:-
you have take page offset by uri segment like:-
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
// it's depends on your uri segments
Now call your model with about page offset
$this->my_model->get_results($page,$something);
Now count total number of rows:-
$cnt=$this->my_model->count_results($something);
if 2nd and 3rd steps is done in one go then you have to do it like..
$temp=array();
$temp['result']=your query result;
$temp['count']=your count result;
Now insert this lines for pagination:-
$this->load->library("pagination");
$config = array();
$config["total_rows"] = $cnt; // or $result['count']
$config["per_page"] = 10;
$config['uri_segment'] = 4;
$config['num_links']=2;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config["base_url"] = your _controller_url
$config["display_pages"]=TRUE;
$this->pagination->initialize($config);
$data["links"] = $this->pagination->create_links();
Now call your view:-
$this-> load->view(your_view_name);
In your view you have to add div
tag like:
<div class="pagination">
<?php echo $links;?>
</div>
Hope this may help you.
Upvotes: 0