【问题标题】:codeigniter's pagination offset not workingcodeigniter 的分页偏移不起作用
【发布时间】:2013-04-28 21:11:37
【问题描述】:

在 CI 的分页中,数据索引应该跟随偏移量。例如:如果限制是 10,那么第二个索引应该有 10 个偏移量,这意味着索引将从 11 开始到 20。

我遵循了一些教程,但我仍然无法让这个偏移量正常工作。 每次点击不同的分页索引时,我的索引总是重置为 1

这是我的分页代码,注意我试图回显 $offset 并且值为 true(在第二个索引 = 10,在第三个索引 = 20,$limit = 10) 所以我不知道为什么它不起作用

public function index($offset = 0) {
        //check authorization
        if(!isset($_SESSION['username']))
            redirect('backend_umat/login');

            // the $offset value is true, but the index is still reseted to 1   
        echo $offset;
        $limit = 10;
        $result = $this->umat_m->get_umat($limit, $offset);

        //pagination
        $config['base_url'] = site_url('/backend_umat/index');
        $config['total_rows'] = $result['num_rows'];
        $config['per_page'] = $limit; 
        $config['uri_segment'] = 3;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';

        $this->pagination->initialize($config); 

        $data['pagination'] = $this->pagination->create_links();

这是我的模型:

public function get_umat($limit, $offset) {
        $this->db->select('*')->from('msumat')->limit($limit, $offset)->
        join('mskelas', 'msumat.kelas_id = mskelas.kelas_id');

$q = $this->db->get();
            $result['rows'] = $q->result();

            $result['num_rows'] = $this->db->select('*')->from('msumat')->
                                  join('mskelas', 'msumat.kelas_id = mskelas.kelas_id')
                                  ->get()->num_rows();

return $result;

感谢您的帮助:D

【问题讨论】:

  • 我看不到你的代码中的错误,如果偏移量是正确的,你的查询呢? echo $this-&gt;db-&gt;last_query() 就在模型中的第一个查询之后。有什么问题,来自 DB 或分页 html 的数据?

标签: codeigniter pagination


【解决方案1】:
    public function index() {
        //check authorization
        if(!isset($_SESSION['username']))
            redirect('backend_umat/login');

        // the $offset value is true, but the index is still reseted to 1   


    //pagination
    $config['base_url'] = base_url().'backend_umat/index';
    // basically you need a separate query to return only numrows
    $config['total_rows'] = ?;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 3;
    $config['full_tag_open'] = '<div id="pagination">';
    $config['full_tag_close'] = '</div>';

    $this->pagination->initialize($config); 
    $offset = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

    $result = $this->umat_m->get_umat( $config['per_page'], $offset);

    $data['pagination'] = $this->pagination->create_links();

在这里试试这个,我改变了一些代码希望这能工作。基本上,在从模型中调用任何内容之前,我首先初始化了分页配置。只需执行单独的查询即可获取行数。

【讨论】:

  • 感谢您的帮助,我会尽快尝试您的代码,并让您知道结果
  • 对不起你的代码不起作用,结果和我的代码一样,$offset 的值是 true 但索引不起作用
  • @BlazeTama 抱歉,但我不明白索引是什么意思?
  • 您的 base_url 中也缺少 /。做这个。 $config['base_url'] = base_url().'backend_umat/index/';
【解决方案2】:

控制器 site.com/&lt;controller&gt;/index/&lt;page&gt;

public function index($offset = 0) {
    //check authorization
    if(!isset($_SESSION['username']))
        redirect('backend_umat/login');

    $limit   = 10;
    $offset = (int) $offset;
    $result  = $this->umat_m->get_umat($limit, $offset);

    //pagination
    $config['base_url']       = site_url('/backend_umat/index');
    $config['total_rows']     = $result['num_rows'];
    $config['per_page']       = $limit; 
    $config['uri_segment']    = 3;
    $config['full_tag_open']  = '<div id="pagination">';
    $config['full_tag_close'] = '</div>';

    $this->pagination->initialize($config); 

    $data['pagination'] = $this->pagination->create_links();

型号

public function get_umat($limit, $offset) {
    $result['rows'] = $this->db
        ->select('SQL_CALC_FOUND_ROWS, msumat.*, mskelas.*', FALSE)
        ->limit($limit, $offset == 1 ? 0 : $offset)
        ->join('mskelas', 'msumat.kelas_id = mskelas.kelas_id')
        ->get('msumat')
        ->result();

    $req = $this->db->query('SELECT FOUND_ROWS()')->row_array(); 
    $result['num_rows'] = $req['FOUND_ROWS()']; 

    return $result;
}

为了不必重写第二个 sql req,如果请求不包含 LIMIT 语句,您可以使用SQL_CALC_FOUND_ROWS 检索总数。但这可能比两个请求慢。

我使用FALSE 作为select() 中的第二个参数,以便CI 不会尝试使用反引号保护字段或表名称。

-&gt;select('*'):如果你想要所有的字段没用,如果没有调用select方法,CI默认会这样做。

-&gt;get()-&gt;num_rows():改用-&gt;count_all_results([table])

【讨论】:

  • 感谢您的帮助。我会尽快尝试你的代码,但是你有更多的“初学者”方法来完成这个吗?我在 nettuts 中观看的教程提供了一个非常简单的代码(就像我的问题中的那个),但是我认为我犯了一个“小”错误,所以偏移量不起作用
  • 好的,但要小心,nettuts 上的所有 tuto 并不总是更新。
【解决方案3】:

首先,感谢 tomexsans 和 lighta 的帮助。抱歉,我犯了一个“愚蠢”的错误——花了大约 2-3 个小时才意识到——索引没有遵循 $offset,因为我忘记使用该变量。我使用一个整数,每次页面加载时我都会重置为 1,因此索引将永远重置为 1:P

【讨论】:

    猜你喜欢
    • 2017-05-03
    • 2013-09-17
    • 1970-01-01
    • 2021-11-11
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多