当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《Codeigniter分页从数据库限制,偏移》经验,为你挑选了1个好方法。

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

例如,我有以下URL:localhost/statistics/api_based 导航时,显示$ query结果,生成链接,OK.但是,当我导航到第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页将显示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'] = '
    '; $config ['full_tag_close'] = '
'; $config ['prev_link'] = '«'; $config ['prev_tag_open'] = '
  • '; $config ['prev_tag_close'] = '
  • '; $config ['next_link'] = '»'; $config ['next_tag_open'] = '
  • '; $config ['next_tag_close'] = '
  • '; $config ['cur_tag_open'] = '
  • '; $config ['cur_tag_close'] = '
  • '; $config ['num_tag_open'] = '
  • '; $config ['num_tag_close'] = '
  • '; $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> random_user_..:

    你可能对工作方式有误解LIMIT.

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

    LIMIT 10
    

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

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

    LIMIT 20, 10
    

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

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

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

    推荐阅读
    勤奋的瞌睡猪_715
    这个屌丝很懒,什么也没留下!
    DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
    Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有