Php 来自数据库的Codeigniter分页限制,偏移量

Php 来自数据库的Codeigniter分页限制,偏移量,php,codeigniter,pagination,Php,Codeigniter,Pagination,我在CI 3.0中启动了一个web应用程序,一切都很顺利,我的分页工作正常,但有一个问题我无法解决 例如,我有以下URL:localhost/statistics/api\u-based 当显示导航$查询结果时,将生成链接,确定。 但是,例如,当我导航到第2页时,URL将变成: localhost/statistics/dll_-based/index/2和第3页将成为localhost/statistics/api_-based/index/3,因此我使用段。 现在的问题是生成的查询: SEL

我在CI 3.0中启动了一个web应用程序,一切都很顺利,我的分页工作正常,但有一个问题我无法解决

例如,我有以下URL:localhost/statistics/api\u-based 当显示导航$查询结果时,将生成链接,确定。 但是,例如,当我导航到第2页时,URL将变成: localhost/statistics/dll_-based/index/2和第3页将成为localhost/statistics/api_-based/index/3,因此我使用段。 现在的问题是生成的查询:

SELECT * FROM `shield_api` ORDER BY `id` ASC limit 20 offset 2
偏移量2-是页码2,如果我没记错的话,应该是20。 我每页显示20个结果,所以第一页正确地显示了1-20的结果,但是第二页将显示2-21的结果,所以你明白我的意思了,这不好

这是我的控制器:

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 );
    }

有人能指出我的问题吗?非常感谢您的帮助。

您可能对限制的工作原理有误解

一个值的限制设置要返回的最大数量

LIMIT 10
LIMIT 20, 10
将检索最多10行,从匹配查询的结果的开头开始

使用两个值设置开始位置偏移量(以行为单位,而不是以页数为单位)以及要返回的最大行数

LIMIT 10
LIMIT 20, 10
将检索最多10行,从第20行开始

因此,您需要在此处稍微修改一下逻辑:

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

您可能对限制的工作原理有误解

一个值的限制设置要返回的最大数量

LIMIT 10
LIMIT 20, 10
将检索最多10行,从匹配查询的结果的开头开始

使用两个值设置开始位置偏移量(以行为单位,而不是以页数为单位)以及要返回的最大行数

LIMIT 10
LIMIT 20, 10
将检索最多10行,从第20行开始

因此,您需要在此处稍微修改一下逻辑:

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

$offset=$offset-1*$每页$offset=$offset-1*$每页如果查看我的索引$offset=0,您将看到偏移量成为页码。。。所以如果页码是3,那么提供3。。。lolI认为你解决了我的问题,第一个查询:选择*来自shield\u api ORDER BY id ASC LIMIT 0,20,第二个页面查询:选择*来自shield\u api ORDER BY id ASC LIMIT 20,20如果你查看我的索引$offset=0,你会看到偏移量变成了页码。。。所以如果页码是3,那么提供3。。。lolI认为您解决了我的问题,第一个查询:按id ASC限制从shield\u api订单中选择*0,20,第二个页面查询:按id ASC限制从shield\u api订单中选择*20,20