Show Cognitive Line Show Number of Results & Page
I am using Codeigniter 3 and have a simple PHP application.
Using the pagination class, I would like to display the following at the top of each of my pages;
Mapping x to y from z results
Where
x = start row
y - end row
z = total rows
`Showing 1 to 10 of 5213 results.`
`Showing 11 to 20 of 5213 results.`
`etc`
I can get complete lines using a variable $config['total_rows']
. Not sure what the rest is.
My items.php controller looks like this:
public function index() {
$config['base_url'] = '/items/index';
$config['use_page_numbers'] = FALSE;
$config['reuse_query_string'] = TRUE;
$config['total_rows'] = $this->db->get('item')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 10;
$config['full_tag_open'] = '<div><ul class="pagination">';
$config['full_tag_close'] = '</ul></div><!--pagination-->';
$config['first_link'] = '« First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last »';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';
$config['next_link'] = 'Next →';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '← Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';
$config['anchor_class'] = 'follow_link';
$this->load->library('pagination');
$this->pagination->initialize($config);
$data = array(
'items' => $this->items_model->itemList()
);
$this->load->view('item_list', $data);
}
My urls are in the following format:
items/index // displays results 1-10
items/index/10 // displays results 11-20
items/index/20 // displays results 21-30
Any help would be greatly appreciated. Thanks to
+3
source to share
2 answers
$data['z'] = $config['total_rows'];
$data['x'] = (int)$this->uri->segment(3) + 1;
if ($this->uri->segment(3) + $config['per_page'] > $config['total_rows']) {
$data['y'] = $config['total_rows'];
} else {
$data['y'] = (int)$this->uri->segment(3) + $config['per_page'];
}
Also, you can do one pre-load check if the requested page is in range, and if it is not, you must redirect the visitor to the first or last page as per your requirement.
+2
source to share
Place below code in your controller below pagination function: -
$start= (int)$this->uri->segment(3) * $config['per_page']+1;
$end = ($this->uri->segment(3) == floor($config['total_rows']/ $config['per_page']))? $config['total_rows'] : (int)$this->uri->segment(3) * $config['per_page'] + $config['per_page'];
$data['result_count']= "Showing ".$start." - ".$end." of ".$config['total_rows']." Results";
In your opinion: -
<?php echo $result_count;?>
+1
source to share