Codeigniter Pagination From Database limit, offset

2019-08-12 19:05发布

问题:

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'] = '&laquo;';
        $config ['prev_tag_open'] = '<li>';
        $config ['prev_tag_close'] = '</li>';
        $config ['next_link'] = '&raquo;';
        $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.

回答1:

You may have a misunderstanding of how LIMIT works.

Limit with one value sets the maximum number to be returned.

LIMIT 10

Will retrieve at most 10 rows, starting at the beginning of the results matching your query.

Limit with two values sets the start position (offset in rows, not pages) and the maximum number of rows to be returned.

LIMIT 20, 10

Will retrieve at most 10 rows, starting at row 20.

So, you'll need to modify your logic a bit here:

$start = max(0, ( $offset -1 ) * $per_page);
$qry .= ' LIMIT {$start}, {$per_page}';