Pagination do not correct display page numbers Codeigniter

前端 未结 3 962
天涯浪人
天涯浪人 2021-01-02 23:31

My controller function

function test($start_from = 0)
{
    $this->load->library(\'pagination\');

    $data = array();

    $per_page = 3;
    $total          


        
3条回答
  •  醉话见心
    2021-01-03 00:08

    I managed to do this without modifying the class. The best way will be to make a copy of the pagination class, make your changes and use it. This way if you update CI, you won't lose the modification. Here is my solution without modifying the class.

    First I want to say that using only the config option $config['use_page_numbers'] = TRUE will also do the trick but not entirely. The things I found out not working using only this option are the following:
    if you try to edit the url bar pages manually it treats them like offset not like pages and also if you try to go back from page 2 to page 1 using the "prev" link it also treats the page number like an an offset.

    The code:

    $config['base_url'] = base_url('my/url/page');
    $config['total_rows'] = count($this->my_model->get_all());
    $config['per_page'] = 2;
    $config['use_page_numbers'] = TRUE;     
    $config['uri_segment'] = 4; 
    
    //i'm looading the pagination in the constuctor so just init here
    $this->pagination->initialize($config); 
    
    if($this->uri->segment(4) > 0)
        $offset = ($this->uri->segment(4) + 0)*$config['per_page'] - $config['per_page'];
    else
        $offset = $this->uri->segment(4);
    //you should modify the method in the model to accept limit and offset or make another function - your choice       
    $data['my_data'] = $this->my_model->get_all($config['per_page'], $offset);
    

    This way
    page = false (my/url) or (my/url/page) - basically if the 4th uri segment is false,
    page = 0 (my/url/page/0),
    and
    page = 1 (my/url/page/1)

    will all display the first page and then the other links will be working fine. I'm also validating the page e.g - if someone wants to enter (my/url/page/2323) this will throw an error and in the model you should check if the result is false and if it is the controller should show an error page or something. Hope this helps.

提交回复
热议问题