2017-05-25 195 views
3

我是Codeigniter的初学者,一切正常,但我遇到了一个问题,我搜索了一个正确的结果,但是我按下了第二页,出现错误,我搜索到很多,看见一个话题谈论会议,但没有和我一起工作分页第二页后搜索结果不起作用

这是我的控制器

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'); 
     } 
    } 

这就是我的模型

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(); 
     } 
    } 

回答

0

试试这个,我测试了它和工作,运气好的话

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

它运行良好,但是当我搜索小于2的记录时,结果只有1行! ,请任何人帮忙 – Syam

1

你误sed把一个变量分页到控制器上。

public function search($page_num = 1) { 

然后,而不是检查URI的段(if ($this->uri->segment(2))) 检查传入的页码。

因为路由时间发生错误,所以Codeigniter在执行'search'方法之前给出错误。

+0

我不工作,当我得到结果在第一页,我发现链接路径第二页is'http:// localhost/elvan/CustomReport/search /?per_page = 5' – Syam

+0

'if this($ this-> uri-> segment(2))'this correct, 这个分页在主页中很好用,但问题是分页丢失了搜索结果在第二页 – Syam