How to use pagination in codeigniter with $this->db->query()?

后端 未结 1 720
青春惊慌失措
青春惊慌失措 2021-01-28 02:06

I am developing a project on codeigniter, I am facing difficulty while creating pagination of records.

Actually, when I user $this->db

相关标签:
1条回答
  • 2021-01-28 02:39

    Getting Started with CodeIgniter Pagination

    Regardless of what you are paginating, you will normally want a URI segment designating a page, unless you want your URI segment to designate an offset. In my example, I'll assume the desire for pages.

    So, lets say we want to paginate some foos, and our database contains a table called foos. Pretend we have plenty of foos (65). In our Foos controller, we have some methods:

    public function index()
    {
        $this->page();
    }
    
    public function page( $page = 1 )
    {
        // Here we get and show our foos
    }
    

    When we want to get results for the current page of foos, we have a page number parameter, and that's how we know what set of foos to get, but first we also need a total count of our foos. So inside our page method, we can call a method on the model to do all the work:

    public function page( $page = 1 )
    {
        // Here we get and show our foos
        $this->load->model('foos_model');
        $view_data['foos'] = $this->foos_model->get_foos( $page );
    }
    

    The Work is All Done in the Model

    Remember, in the model we need a total count of our foos, pagination link creation, and then the foos for this page. Let's start with counting foos:

    public function get_foos( $page )
    {
        // First count all foos
        $count = $this->db->count_all('foos');
    }
    

    Next, create the pagination links. Let's say we want 10 foos per page:

    public function get_foos( $page )
    {
        // First count all foos
        $count = $this->db->count_all('foos');
    
        // Create the pagination links
        $this->load->library('pagination');
        $this->load->helper('url');
    
        $paging_conf = [
            'uri_segment'      => 3,
            'per_page'         => 10,
            'total_rows'       => $count,
            'base_url'         => site_url('foos/page'),
            'first_url'        => site_url('foos'),
            'use_page_numbers' => TRUE
        ];
        $this->pagination->initialize($paging_conf);
    
        // Create the paging buttons for the view
        $this->load->vars('pagination_links', $this->pagination->create_links());
    }
    

    Now it's time to get the actual set of foos we will display. Notice the calculation of the offset:

    public function get_foos( $page )
    {
        // First count all foos
        $count = $this->db->count_all('foos');
    
        // Create the pagination links
        $this->load->library('pagination');
        $this->load->helper('url');
    
        $paging_conf = [
            'uri_segment'      => 3,
            'per_page'         => 10,
            'total_rows'       => $count,
            'base_url'         => site_url('foos/page'),
            'first_url'        => site_url('foos'),
            'use_page_numbers' => TRUE
        ];
        $this->pagination->initialize($paging_conf);
    
        // Create the paging buttons for the view
        $this->load->vars('pagination_links', $this->pagination->create_links());
    
        // The pagination offset
        $offset = $page * $paging_conf['per_page'] - $paging_conf['per_page'];
    
        // Get our set of foos
        $query = $this->db->get('foos', $paging_conf['per_page'], $offset);
    }
    

    Finally, make sure there were foos before passing them back to the controller:

    public function get_foos( $page )
    {
        // First count all foos
        $count = $this->db->count_all('foos');
    
        // Create the pagination links
        $this->load->library('pagination');
        $this->load->helper('url');
    
        $paging_conf = [
            'uri_segment'      => 3,
            'per_page'         => 10,
            'total_rows'       => $count,
            'base_url'         => site_url('foos/page'),
            'first_url'        => site_url('foos'),
            'use_page_numbers' => TRUE
        ];
        $this->pagination->initialize($paging_conf);
    
        // Create the paging buttons for the view
        $this->load->vars('pagination_links', $this->pagination->create_links());
    
        // The pagination offset
        $offset = $page * $paging_conf['per_page'] - $paging_conf['per_page'];
    
        // Get our set of foos
        $query = $this->db->get('foos', $paging_conf['per_page'], $offset);
    
        // Make sure we have foos
        if( $query->num_rows() > 0 )
            return $query->result();
    
        // Else return default
        return NULL;
    }
    

    Back in our controller, we can pass the foos to the view:

    public function page( $page = 1 )
    {
        // Here we get and show our foos
        $this->load->model('foos_model');
        $view_data['foos'] = $this->foos_model->get_foos( $page );
    
        // Load the view and pass in the foos
        $this->load->view('foos_view', $view_data);
    }
    

    Display the pagination links and foos

    In the view, we now have the ability to display our pagination links and foos:

    echo $pagination_links;
    
    if( ! empty( $foos ) )
    {
        foreach( $foos as $foo )
            echo $foo->name . '<br />';
    }
    

    Conclusion

    Paginating things with CodeIgniter is really easy. If you have doubts or problems, just read CodeIgniter's documentation: https://www.codeigniter.com/userguide3/libraries/pagination.html?highlight=pagination

    Extra info regarding the use of $this->db->query() vs $this->db->get()

    Functionally, as long as you don't make mistakes, the use of $this->db->query() would produce the same results as $this->db->get(). In the case of my example, it would look like this:

    // $query = $this->db->get('foos', $paging_conf['per_page'], $offset);
    
    $query = $this->db->query(
        'SELECT *
        FROM foos
        LIMIT ' . $offset . ', ' . $paging_conf['per_page'] );
    
    0 讨论(0)
提交回复
热议问题