How to create Pagination in Codeiginter?

后端 未结 4 1737
傲寒
傲寒 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:04

    For a better use of pagination create a function in your helper. That will very useful for you for future use. you don't ever need to load library and other values. fix this function in your helper.

    if( ! function_exists('createPaginationForm')) {
        function createPaginationForm($url, $totalRows, $perPage, $segment, $queryString = '', $config = array()) {
            $CI =& get_instance();
            $CI->load->library('pagination');
            $config['base_url'] = base_url().$url;
            $config['total_rows'] = $totalRows;
            $config['per_page'] = $perPage;
            $config['uri_segment'] = $segment;
            $CI->pagination->initialize($config);
            return $CI->pagination->create_links($queryString);
        }
    }
    

    and use in your controller .

    var $perPage = '10';
    var $segment = '4';
    $total_result = "50";
    $pagination = createPaginationForm('admin/news/index/',$total_result ,$this->perPage, $this->segment);
    $this->data['pagination'] = $pagination;
    

    and print this in your view.

    echo $pagination
    
    0 讨论(0)
  • 2020-12-22 04:05

    you need to some data to displat in pagination use query for this and $config['total_rows'] so it will display total result pagination with 5 records

    in my case i am using $this->Account->search_users($where,false, false,false,true,false); is a model function will return array of result

    return $query->result_array();
    

    controller :-

          $this->load->library('pagination');   
        $config['total_rows'] = $this->Account->search_users($where,false, false,false,true,false);
        $config['per_page']= 5;
        $config['uri_segment'] = 3;
        $config['base_url']= base_url().'/app/index'; 
        $config['suffix'] = '?'.http_build_query($_GET, '', "&"); 
        $this->pagination->initialize($config);
        $this->data['paginglinks'] = $this->pagination->create_links();     
        $this->data['per_page'] = $this->uri->segment(3);       
        $this->data['offset'] = $offset ;
    

    view:-

    <div class="pagination" style="float:right;"> <?php echo $paginglinks; ?></div>
    

    For more info :- http://ellislab.com/codeigniter/user-guide/libraries/pagination.html

    my model function is :-

    public function search_users($where = false, $num = false, $offset = false, $order_by = false, $count = false, $select = false , $table=false){
    
        if(!$select){$select = 'U.*';}
    
            if(isset($where['where']) && !empty($where['where'])) {
                $this->db->where($where['where']);
            }
    
            if($order_by){
                $this->db->order_by($order_by[0], $order_by[1]);
            }else{
                $this->db->order_by('U.u_id', 'DESC');
            }
    
            $query = $this->db->get($table . ' AS U');
            //print_r($this->db->last_query()).'<hr>';
            $recount = $query->num_rows;    
            if($count) {
                return $recount;    
            }
    
            if($recount > 0){
                return $query->result_array();
            }
            else{ 
                return false;
            }
        }
    
    0 讨论(0)
  • 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'] = '<ul>';
                $config['full_tag_close'] = '</ul>';
                $config['cur_tag_open'] = '<li class="active"><a href="">';
                $config['cur_tag_close'] = '</a></li>';
                $config['num_tag_open'] = '<li>';
                $config['num_tag_close'] = '</li>';
                $config['next_tag_open'] = '<li>';
                $config['next_tag_close'] = '</li>';
                $config['prev_tag_open'] = '<li>';
                $config['prev_tag_close'] = '</li>';
                $config['first_link'] = '&lt;&lt;';
                $config['first_tag_open'] = '<li>';
                $config['first_tag_close'] = '</li>';
                $config['last_link'] = '&gt;&gt;';
                $config['last_tag_open'] = '<li>';
                $config['last_tag_close'] = '</li>';
                $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

      <div id="pagination"><?php echo $pagination;?></div>
    
    0 讨论(0)
  • 2020-12-22 04:18

    Controller:

    public function all_students() 
     {      $this->load->model('loginmodel');
            $config['anchor_class'] = '';
            $config['show_count'] = true;
            $config['base_url'] = site_url('controllername/all_students');
            $config['total_rows'] = sizeof($this->loginmodel->students_list());
    
            $data['students_data']= $this->loginmodel->students_list();
            $data['dropdown']=      $this->loginmodel->get_degree_names();
            $config['per_page'] = 10;
            $this->jquery_pagination->initialize($config);
            $data['links'] = $this->jquery_pagination->create_links();
            $data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
            $data['details'] = $this->loginmodel->students_list($config['per_page'], $data['page']);
            $this->load->view('all_students', $data);
        }
    

    Model

    public function students_list($limit=NULL,$start=NULL)
    {
    
    
        $this->db->select('*');
        $this->db->from('student'); 
    
        $this->db->limit($limit,$start);                
    
        $query= $this->db->get();
    
        return $query->result();
    
     }
    
    0 讨论(0)
提交回复
热议问题