Why are pagination links not working for me in CodeIgniter?

后端 未结 2 1470
醉梦人生
醉梦人生 2021-01-27 08:58

I\'m trying to use codeigniter pagination for my products so there are multiple pages which products but its not working for me and I don\'t know why..

This is my pagina

相关标签:
2条回答
  • 2021-01-27 09:08

    Please make some changes as follows:

    Controller:

    public function pagination($row = 0) {
         //load pagination library
            $this->load->library('pagination');
            //laad Products_model voor pagination
            $this->load->model('Pagination_model');
            $this->load->model('Product_model');
            $config = array();
            $limit = '24';      
            $config['base_url'] = site_url() . '/AlleCadeausController/pagination';
            $config['full_tag_open'] = "<ul class='pagination'>";
            $config['full_tag_close'] = '</ul>';
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';
            $config['cur_tag_open'] = '<li class="active"><a href="#">';
            $config['cur_tag_close'] = '</a></li>';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';
            $config['prev_link'] = '<i class="fa fa-long-arrow-left"></i>Previous Page';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['next_link'] = 'Next Page<i class="fa fa-long-arrow-right"></i>';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';     
            $config["total_rows"] = $this->Product_model->record_count();            
            $config['per_page'] = $limit;
            $this->pagination->initialize($config);  
            $data['links'] = $this->pagination->create_links();
            $data['products'] = $this->Product_model->selectProducts($row,$limit);
            $this->load->view("allecadeaus", $data);
        }
    

    Model:

    function selectProducts($row,$limit)
            {   
                $query = $this->db->get('Your_table_name',$limit,$row);
    
                if ($query->num_rows() > 0){
                  return $query->result_array();
                }
                 else {
                  return array();
                }
    
            }
    
    function record_count(){        
            $this->db->from('Your_table_name');         
            $query = $this->db->get();
            $row = $query->num_rows();
            return $row;
        }
    

    This will help you..thanks!

    0 讨论(0)
  • 2021-01-27 09:14

    You have a mix of code, pagination and not pagination results.

    Add,

    $config["num_links"] = 5; //Number of links in pagination
    

    After initilize Codeigniter pagination library. You have to execute a pagination query in your database, for example:

    $where = "IF you have got";
    $orderby "IF you have got";
    
    $data['products'] = $this->Pagination_model->select_products($config["per_page"], $this->uri->segment(3), $where, $orderby);
    

    Then in your model Pagination_model, you should have got this:

    public function select_products($per_page, $segment, $where, $orderby)
    {
        if (!isset($segment)) $segment = 1;
    
        $init= ($segment - 1) *  $per_page;
    
        $sql = "SELECT *
                  FROM Your_table
                  $where 
                  ORDER BY $orderby
                  OFFSET $init ROWS
                  FETCH NEXT $per_page ROWS ONLY";
    
        $query= $this->db->query($sql);
        return $query->result_array();
      }
    

    Be sure you are right point to this->uri->segment() is 3. I think you dont have pagination links because you dont have results in your query.

    0 讨论(0)
提交回复
热议问题