我是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'] = '«';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$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(' ', $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 :(得分:1)
你错过了为控制器添加分页变量。
public function search($page_num = 1) {
然后,而不是检查URI的片段(if ($this->uri->segment(2))
)
检查传入的页码。
因为,路由时发生错误,因此Codeigniter在执行“搜索”方法之前会出错。
答案 1 :(得分: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'] = '«';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$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(' ', $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');
}
}