dsklfsdlkf1232 2017-08-17 18:06
浏览 34
已采纳

Codeigniter 3分页错误:第二页显示记录3到13而不是11到20

I am making a Codeigniter 3 application, consisting of a table (with approximately 100 rows) that I have paginated, 10 rows per page.

In the home controller I have:

public function index() {
    $this->load->model('Customer');
    $this->load->library('pagination');
    $config = [
        'base_url' => base_url("index.php"),
        'per_page' => 10,
        'total_rows' => $this->Customer->get_num_rows(),
        'uri_segment' => 3,
        'first_tag_open' =>  '<li>',
        'first_tag_close' => '</li>',
        'last_tag_open' =>  '<li>',
        'last_tag_close' => '</li>',
        'full_tag_open' =>  '<ul class="pagination">',
        'full_tag_close' => '</ul>',
        'next_tag_open' =>  '<li>',
        'next_tag_close' => '</li>',
        'prev_tag_open' =>  '<li>',
        'prev_tag_close' => '</li>',
        'num_tag_open' =>   '<li>',
        'num_tag_close' =>  '</li>',
        'cur_tag_open' =>   '<li class="active"><a>',
        'cur_tag_close' =>  '</a></li>',
    ];
    $this->pagination->initialize($config);
        $customers = $this->Customer->getCustomers($config['per_page'], $this->input->get('page'));
    $this->load->view('home', ['records'=>$customers]);
}

In the Model file I have:

class Customer extends CI_Model {
 public function __construct() {
    $this->load->database();
 }

 public function getCustomers($limit, $offset) {
    $this->db->limit($limit, $offset);
    $query = $this->db->get('customers');
    return $query->result();
 }
}

The view:

<div class="pagination-container text-center">
    <?php echo $this->pagination->create_links(); ?>
</div> 

The first page looks good (I have recieved help from the comunuty with that. Thank you!): First page

The problem is that the second page shows records 3 to 13 instead of 11 to 20: Shows records 3 to 13 instead of 11 to 21

  • 写回答

4条回答 默认 最新

  • dpx86402 2017-08-18 18:09
    关注

    Inside the Home controller, after the changes that made the pagination work, I now have:

    public function index() {
        $this->load->model('Customer');
        $this->load->library('pagination');
        $config = [
            'base_url' => base_url("index.php"),
            'page_query_string' => TRUE,
            'query_string_segment' => 'page',
            'display_pages' => TRUE,
            'use_page_numbers' => TRUE,
            'per_page' => 10,
            'total_rows' => $this->Customer->get_num_rows(),
            'uri_segment' => 3,
            'first_link' => '&laquo;',
            'first_tag_open' =>  '<li>',
            'first_tag_close' => '</li>',
            'last_link' => '&raquo;',
            'last_tag_open' =>  '<li>',
            'last_tag_close' => '</li>',
            'full_tag_open' =>  '<ul class="pagination">',
            'full_tag_close' => '</ul>',
            'next_link' =>  '&rsaquo;',
            'next_tag_open' =>  '<li>',
            'next_tag_close' => '</li>',
            'prev_link' => '&lsaquo;',
            'prev_tag_open' =>  '<li>',
            'prev_tag_close' => '</li>',
            'num_tag_open' =>   '<li>',
            'num_tag_close' =>  '</li>',
            'cur_tag_open' =>   '<li class="active"><a>',
            'cur_tag_close' =>  '</a></li>'
        ];
        if (!isset($_GET[$config['query_string_segment']])) {
            $_GET[$config['query_string_segment']] = 1;
        }
        $limit = $config['per_page'];
        $offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
        $this->pagination->initialize($config);
        $customers = $this->Customer->getCustomers($limit, $offset);
        $this->load->view('home', ['records'=>$customers]);
    }
    

    It works fine, I hope it will help many. But I wonder if I could be simplified, written with less code...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看