Pagination in code igniter

2019-09-15 18:25发布

问题:

I am do pagination in code igniter ,join two tables for Search purpose

my Models's

public function main_search_count($term)
    {
 if ($term == "NIL") $term = "";
 $this->db->select('*');    
 $this->db->from('tbl_doctor');  
 $this->db->join("tbl_specialisation", "tbl_specialisation.spec_id = tbl_doctor.spec_id",'left');
$this->db->where("(tbl_doctor.dr_name LIKE '%".$term."%' OR tbl_doctor.district LIKE '%".$term."%' OR tbl_specialisation.spec_specialise LIKE '%".$term."%')");
$query= $this->db->get();
return $query->num_rows();
    }
    public function fetch_data($limit, $offset, $term)
    {
if ($term == "NIL") $term = "";
$this->db->select('*');    
$this->db->from('tbl_doctor');  
$this->db->join("tbl_specialisation", "tbl_specialisation.spec_id =         tbl_doctor.spec_id",'left');
$this->db->where("(tbl_doctor.dr_name LIKE '%".$term."%' OR tbl_doctor.district LIKE '%".$term."%' OR tbl_specialisation.spec_specialise LIKE '%".$term."%')");
$this->db->limit($limit, $offset);
$query = $this->db->get();
if($query->num_rows()>0)
{
 return $query;
 }   
 }
my controller

 function get_quick_doct($offset = 0)
    {
$term = ($this->input->post("term"))? $this->input->post("term") : "NIL";
$term = ($this->uri->segment(3)) ? $this->uri->segment(3) : $term;
$config = array();
   $config["base_url"] = base_url() . "User/get_quick_doct/$term";
    $config['total_rows'] = $this->Search_model->main_search_count($term);
    $config['per_page'] = 5;
    $config['full_tag_open'] = '<ul class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['first_link'] = false;
    $config['last_link'] = false;
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = 'Prev';
    $config['prev_tag_open'] = '<li class="prev">';
    $config['prev_tag_close'] = '</li>'; 
    $config['next_link'] = 'Next';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $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>';
    $this->pagination->initialize($config);
$data['quick_doc'] = $this->Search_model->fetch_data($config['per_page'], $offset,$term);
$data['get_specs'] = $this->specialisation_model->get_specialisation();
$data['get_specs'] = $this->specialisation_model->specialisation_get();
if($data){
$this->load->helper(array('form', 'url'));
$this->load->view('customer/header', $data);
$this->load->view('customer/side_view',$data);
$this->load->view('customer/quick_search',$data);
$this->load->view('customer/footer');
        }
else{
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">No sdfdsfds Results available for your search!!!</div>');
$this->load->view('customer/header', $data);
$this->load->view('customer/side_view',$data);
$this->load->view('customer/main_search');
 $this->load->view('customer/footer');
}}

here the problem is the pagination work only one page click next link show same item here show only 5 content only totally more than 5 item but next page show same element

回答1:

Try the function like this, it will definitely solve your problem. Again I am requesting you to go for the documentation for more understanding.

function get_quick_doct()
{
    $term = ($this->input->post("term"))? $this->input->post("term") : "";

    $config = array();
    $config["base_url"] = base_url() . "User/get_quick_doct/";
    $config['total_rows'] = $this->Search_model->main_search_count($term);
    $config['per_page'] = 5;
    $config['full_tag_open'] = '<ul class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['first_link'] = false;
    $config['last_link'] = false;
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = 'Prev';
    $config['prev_tag_open'] = '<li class="prev">';
    $config['prev_tag_close'] = '</li>'; 
    $config['next_link'] = 'Next';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $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>';
    $this->pagination->initialize($config);

    $segment = $this->uri->segment (2); // 2 or 3 -- as per your url depth

    $data['quick_doc'] = $this->Search_model->fetch_data($config['per_page'], $segment, $term);
    $data['get_specs'] = $this->specialisation_model->get_specialisation();
    $data['get_specs'] = $this->specialisation_model->specialisation_get();
    if($data)
    {
        $this->load->helper(array('form', 'url'));
        $this->load->view('customer/header', $data);
        $this->load->view('customer/side_view',$data);
        $this->load->view('customer/quick_search',$data);
        $this->load->view('customer/footer');
    }
    else{
        $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">No sdfdsfds Results available for your search!!!</div>');
        $this->load->view('customer/header', $data);
        $this->load->view('customer/side_view',$data);
        $this->load->view('customer/main_search');
        $this->load->view('customer/footer');
    }
}