2017-05-25 194 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