I am, using the Codeigniter pagination library,
I am wondering how I can grab the number of the first and last items displayed using the pagination class?
So
itachi's solution works great except you need to handle the case where the last page has less than $per_page elements.
if ($result_end > $total) {
$result_end = $total;
}
In CodeIgniter Controller file
$params['limit'] = 15; $params['offset'] = ($this->input->get('per_page')) ? $this->input->get('per_page') : 0;
$config = $this->config->item('pagination');
$config['per_page'] = $params['limit'];
$config['base_url'] = site_url('cy_controller/action?');
$config['total_rows'] = $this->abcd_model->get_count($params);
$this->pagination->initialize($config);
$data['merchant'] = $this->abcd_model->get_all($params);
$countMerchant = count($data['merchant']);
if ($params['offset'] == 0) {
$find_total_record = $countMerchant;
} else {
$valuec = $params['offset'] + $params['limit'];
if ($valuec > $config['total_rows'])
$find_total_record = $params['offset'] + $countMerchant;
else
$find_total_record = $params['offset'] + $params['limit'];
}
$per_page_total = $find_total_record;
$initial = $params['offset'] == 0 ? 1 : $params['offset'];
$data['showing'] = "Showing " . $initial . " to " . $per_page_total . " of " . $config['total_rows'] . " results";
$data['_view'] = 'cy_controller/action';
$this->load->view('layouts/main', $data);
And in view file
<div class="pull-left">
<?php echo $showing; ?>
</div>
Keeping it simple.
You need 3 variables. Result start, result end (in the page, not the whole) and total result.
You already know the total results (from the pagination). Let's call it $total
.
so, now get the current page ($curpage
) value from CI instance. Then,
$result_start = ($curpage - 1) * $per_page + 1;
if ($result_start == 0) $result_start= 1; // *it happens only for the first run*
for $result_end
, you just need to add the per page value but considering it'll be 1 less,
$result_end = $result_start+$per_page-1;
if ($result_end < $per_page) // happens when records less than per page
$result_end = $per_page;
else if ($result_end > $total) // happens when result end is greater than total records
$result_end = $total;
send all those 3 values to view.
echo "displaying $result_start to $result_end of $total";