Codeigniter分页从数据库限制,偏移

时间:2016-01-03 17:19:23

标签: php codeigniter pagination

我在CI 3.0中启动了一个Web应用程序,一切顺利,我的分页工作正常,但有一个我无法弄清楚的问题......

例如,我有以下网址: localhost / statistics / api_based 导航时,会显示$ query结果,生成链接,OK。 但是,当我导航到第2页时,网址将变为: 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页将显示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 );
    }

有人能指出我的问题吗?任何帮助都是相关的。

1 个答案:

答案 0 :(得分:6)

您可能对LIMIT的工作原理有误解。

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

LIMIT 10

最多可检索10行,从匹配查询的结果开始。

使用两个值的限制设置起始位置(行中的偏移,而不是页面)和要返回的最大行数。

LIMIT 20, 10

从第20行开始,最多可检索10行。

所以,你需要在这里修改一下你的逻辑:

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