I started a web application in CI 3.0, everything working smooth, I got my pagination working, but there is a problem wich I cannot figure out...
For example, I have the following URL: localhost/statistics/api_based When navigate, $query results are displayed, links are generated, OK. But, when I navigate to page 2 for example, the URL will become: localhost/statistics/dll_based/index/2 and page 3 will become localhost/statistics/api_based/index/3 , so therefore I am using segments. Now the problem is the query that is generated:
SELECT * FROM `shield_api` ORDER BY `id` ASC limit 20 offset 2
offset 2 - is the page number 2, and it should be 20, if I am correct. I am displaying 20 results per page, so first page is correctly showing results from 1 - 20, but then page 2 will display results from 2 - 21, so you get my point, it`s not OK...
Here is my controller:
function index($offset = 0) {
// Enable SSL?
maintain_ssl ( $this->config->item ( "ssl_enabled" ) );
// Redirect unauthenticated users to signin page
if (! $this->authentication->is_signed_in ()) {
redirect ( 'account/sign_in/?continue=' . urlencode ( base_url () . 'statistics/api_based' ) );
}
if ($this->authentication->is_signed_in ()) {
$data ['account'] = $this->account_model->get_by_id ( $this->session->userdata ( 'account_id' ) );
}
$per_page = 20;
$qry = "SELECT * FROM `shield_api` ORDER BY `id` ASC";
//$offset = ($this->uri->segment ( 4 ) != '' ? $this->uri->segment ( 4 ) : 0);
$config ['total_rows'] = $this->db->query ( $qry )->num_rows ();
$config ['per_page'] = $per_page;
$config ['uri_segment'] = 4;
$config ['base_url'] = base_url () . '/statistics/api_based/index';
$config ['use_page_numbers'] = TRUE;
$config ['page_query_string'] = FALSE;
$config ['full_tag_open'] = '<ul class="pagination">';
$config ['full_tag_close'] = '</ul>';
$config ['prev_link'] = '«';
$config ['prev_tag_open'] = '<li>';
$config ['prev_tag_close'] = '</li>';
$config ['next_link'] = '»';
$config ['next_tag_open'] = '<li>';
$config ['next_tag_close'] = '</li>';
$config ['cur_tag_open'] = '<li class="active"><a href="#">';
$config ['cur_tag_close'] = '</a></li>';
$config ['num_tag_open'] = '<li>';
$config ['num_tag_close'] = '</li>';
$config ["num_links"] = round ( $config ["total_rows"] / $config ["per_page"] );
$this->pagination->initialize ( $config );
$data ['pagination_links'] = $this->pagination->create_links ();
//$data ['per_page'] = $this->uri->segment ( 4 );
$data ['offset'] = $offset;
$qry .= " limit {$per_page} offset {$offset} ";
if ($data ['pagination_links'] != '') {
$data ['pagermessage'] = 'Showing ' . ((($this->pagination->cur_page - 1) * $this->pagination->per_page) + 1) . ' to ' . ($this->pagination->cur_page * $this->pagination->per_page) . ' results, of ' . $this->pagination->total_rows;
}
$data ['result'] = $this->db->query ( $qry )->result_array ();
$this->load->view ( 'statistics/api_based', isset ( $data ) ? $data : NULL );
}
Can someone point me to my problem ? Any help is apreciated.