Search result does not work after second pagination page

I am new to Codeigniter and everything works well, but I have one problem, I did a search and this answer with correct results, but I click on the second page, I got an error, I searched the topic about session many times but does not work with me

This is my controller

public function search(){
        $this->load->library('pagination');
        $this->load->helper("url");
        $this->load->model(array('CustomReport_m','user','Customer_m')); // This array to save number of lines only
        $username=$this->input->post('select_user');
        $total_row = $this->CustomReport_m->search_count($username);
        $config['page_query_string'] = TRUE;
        $config['base_url'] = base_url().'CustomReport/search/';
        $config['total_rows'] = $total_row;
        $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'] = '&laquo';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo';
        $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);
        if ($this->uri->segment(2)) {
            $page = ($this->uri->segment(2));
        } else {
            $page = 1;
        }
        $str_links = $this->pagination->create_links();
        $data["links"]= explode('&nbsp;', $str_links);
        $data['users']=$this->user->display_users();
        $data['userdata'] = $this->user->userdata();
        $data['customers']= $this->Customer_m->index();
        if(isset($username)and !empty($username)){
              $data["rsl"] =$this->CustomReport_m->search($username,$config["per_page"],$page);
            //$data["rsl"] = $this->CustomReport_m->search($config["per_page"],$page,$username);
            $this->load->view('customreport_v',$data);
        }else{
            redirect('CustomReport','refresh');
        }
    } 

      

This is my model

public function search_count($username) {
        $this->db->like('t_u_id',$username);
        return $this->db->count_all("transactions");
    }
    public function search($username,$limit,$start)
    {
        $this->db->select('*');
        $this->db->limit($limit,$start);
        $this->db->from('transactions');
        $this->db->like('t_u_id',$username);
        $query=$this->db->get();
        if($query->num_rows() > 0){
            return $query->result();
        }else{
            return $query->result();
        }
    }

      

+3


source to share


2 answers


Try This, I tested it and it worked, good luck



   public function search(){
        $this->load->library('pagination');
        $this->load->helper("url");
        $this->load->model(array('CustomReport_m','user','Customer_m')); // This array to save number of lines only
        $username=$this->input->post('select_user');
        $username = ($this->uri->segment(3)) ? $this->uri->segment(3) : $username;
        $total_row = $this->CustomReport_m->search_count($username);
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url("CustomReport/search/$username");
        $config['total_rows'] = $total_row;
        $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'] = '&laquo';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo';
        $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);
        if ($this->uri->segment(4)) {
            $page = ($this->uri->segment(4));
        } else {
            $page = 1;
        }
        $str_links = $this->pagination->create_links();
        $data["links"]= explode('&nbsp;', $str_links);
        $data['users']=$this->user->display_users();
        $data['userdata'] = $this->user->userdata();
        $data['customers']= $this->Customer_m->index();
       if(isset($username)and !empty($username)){
           $this->session->set_flashdata('search',$username);
              $data["rsl"] =$this->CustomReport_m->search($username,$config["per_page"],$page);
            //$data["rsl"] = $this->CustomReport_m->search($config["per_page"],$page,$username);
            $this->load->view('customreport_v',$data);
        }else{
            redirect('CustomReport','refresh');
        }
    }

      

0


source


You missed setting the variable for paging on the controller.

public function search($page_num = 1) {

      



And then instead of checking the segment URI ( if ($this->uri->segment(2))

) Check the incoming page number.

Because the error occurs during routing, so Codeigniter gives an error before executing the lookup method.

+1


source







All Articles