【问题标题】:Codeigniter Pagination From Database limit, offsetCodeigniter 来自数据库的分页限制,偏移量
【发布时间】:2016-04-07 08:50:01
【问题描述】:

我在 CI 3.0 中启动了一个 Web 应用程序,一切正常,我的分页工作正常,但是有一个我无法解决的问题...

例如,我有以下 URL:localhost/statistics/api_based 导航时,显示$query结果,生成链接,OK。 但是,例如,当我导航到 page 2 时,URL 将变为: localhost/statistics/dll_based/index/2page 3 将变为 localhost/statistics/api_based/index/3 ,因此我使用段。 现在的问题是生成的查询:

SELECT * FROM `shield_api` ORDER BY `id` ASC limit 20 offset 2

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

有人可以指出我的问题吗?任何帮助都将不胜感激。

【问题讨论】:

  • $offset = ($offset - 1) * $per_page

标签: php codeigniter pagination


【解决方案1】:

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

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

LIMIT 10

将检索最多 10 行,从与您的查询匹配的结果的开头开始。

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

LIMIT 20, 10

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

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

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

【讨论】:

  • 如果你查看我的索引( $offset = 0 )你会看到偏移量变成了页码......所以如果页码是3,那么offet 3......大声笑跨度>
  • 我想你解决了我的问题,第一个查询:“SELECT * FROM shield_api ORDER BY idASC LIMIT 0, 20”,第二个页面查询:“SELECT * FROM shield_api ORDER BY id ASC 限制 20、20"
猜你喜欢
  • 1970-01-01
  • 2021-11-11
  • 2017-05-03
  • 2023-03-03
  • 2013-09-17
  • 1970-01-01
  • 2016-04-04
  • 2013-06-13
  • 2017-03-27
相关资源
最近更新 更多