How to create Pagination in Codeiginter?

后端 未结 4 1756
傲寒
傲寒 2020-12-22 03:44

I am new to codeigniter, I am trying to create pagination on the records i am fetching from database. I have written the following code, its showing me pagination but its no

4条回答
  •  無奈伤痛
    2020-12-22 04:10

    First Of All i suggest to create a helper function to configure your pagination like this. You gotta find out from codeigniter documention for the option purpose in config

    if (!function_exists('getPaginationConfig')) {
        function getPaginationConfig($url,$total_row,$per_page,$num_links=3){
                $config = array();
                $config['use_page_numbers'] = TRUE;
                $config['uri_segment'] = 3;// depends on how you passing your page number
                $config['base_url'] = $url;
                $config['total_rows'] = $total_row;
                $config['per_page'] = $per_page; 
                $config['full_tag_open'] = '
      '; $config['full_tag_close'] = '
    '; $config['cur_tag_open'] = '
  • '; $config['cur_tag_close'] = '
  • '; $config['num_tag_open'] = '
  • '; $config['num_tag_close'] = '
  • '; $config['next_tag_open'] = '
  • '; $config['next_tag_close'] = '
  • '; $config['prev_tag_open'] = '
  • '; $config['prev_tag_close'] = '
  • '; $config['first_link'] = '<<'; $config['first_tag_open'] = '
  • '; $config['first_tag_close'] = '
  • '; $config['last_link'] = '>>'; $config['last_tag_open'] = '
  • '; $config['last_tag_close'] = '
  • '; $config['num_links'] = $num_links; return $config; } }

    Now On Controller .. you need to get total rows from database and set the perpage in pagination and call with the config here is an example of controller function

     function getMyList($page){
            $limit = YOUR_PAGE_LIMIT;//may be some global variable or input from frontend
            $starting = ($page-1)*$limit;
    
            $data['results']  = $this->your_model->getListOfTable($where,$limit,$starting);
            $total_rows = $this->your_model->getTotalRowsOfTable($where);// where is filter condition
    
            $this -> load -> helper('your_helper');// where yours getPaginationConfig is defined
            $this -> load -> library('pagination');
            $this -> pagination -> initialize(getPaginationConfig(site_url('controller/function'), $total_rows,$limit));
            $data['pagination'] = $this -> pagination -> create_links();
            $this->load->view('yourview',$data);
    

    Your model may look like this

    function getListOfTable($where = array(), $limit, $starting = 0) {
            $this -> db -> select('*');
            // <-- There is never any reason to write this line!
            $this -> db -> from('your_tables');
            if (!empty($where))
               $this -> db -> where($where);
            $this -> db -> limit($limit, $starting);
            $query = $this -> db -> get();
    
            if ($query -> num_rows() > 0) {
                return $query -> result();
            } else {
                return FALSE;
            }
        }
    
    function getTotalRowsOfTable($where = array()){
        if (!empty($data))
             $this -> db -> where($where);
        $this -> db -> from('your_table');
        return $this -> db -> count_all_results();
    
    }
    

    Now On View you just need to echo the pagination where you like

      
    

提交回复
热议问题