Php codeigniter 2中的分页链接变为空结果
我试图在搜索结果中创建分页。我得到了整个搜索结果和正确的分页链接数,但当我试图转到下一页时,搜索结果变成空白。第一页就可以了。但它没有显示任何错误 代码如下: 控制器功能Php codeigniter 2中的分页链接变为空结果,php,pagination,codeigniter-2,Php,Pagination,Codeigniter 2,我试图在搜索结果中创建分页。我得到了整个搜索结果和正确的分页链接数,但当我试图转到下一页时,搜索结果变成空白。第一页就可以了。但它没有显示任何错误 代码如下: 控制器功能 public function staffsearch() { if ($this->session->userdata('logged_in') == FALSE) { $this->index(); } else { $dat
public function staffsearch() {
if ($this->session->userdata('logged_in') == FALSE) {
$this->index();
} else {
$data['action'] = site_url('user/staffsearch/');
$search_by = $this->input->post('search_by');
$keyword = $this->input->post('keyword');
if (!empty($search_by) && !empty($keyword)) {
$uri_segment = 3;
$offset = $this->uri->segment($uri_segment);
$staff_details = $this->User_Model->get_staff_search_result($search_by, $keyword, $this->limit, $offset)->result();
$query = $this->db->query("SELECT * FROM `staff` WHERE `" . $search_by . "` LIKE '%" . $keyword . "%'");
$count = $query->num_rows();
$this->load->library('pagination');
$config['base_url'] = site_url('user/staffsearch/');
$config['total_rows'] = $count;
$config['per_page'] = $this->limit;
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
//$staff_details = $this->User_Model->get_staff_search_result($search_by, $keyword)->result();
$this->load->library('table');
$this->table->set_empty(' ');
$this->table->set_heading('S/No', 'Image', 'Name', 'Office', 'Phone');
$i = 0;
foreach ($staff_details as $staff) {
if ($staff->Office) {
$id = $staff->Office;
$staff_office = $this->User_Model->get_office_by_id($id)->row();
$office = $staff_office->building . ' ' . $staff_office->level . '-' . $staff_office->unit;
} else {
$office = '';
}
if ($staff->Photo_small == '') {
$pic = 'unavailable.jpg';
} else {
$pic = $staff->Photo_small;
}
$this->table->add_row(++$i, '<image src="' . base_url() . 'people/' . $pic . '" width="50" height="50">', anchor('user/staffdetails/' . $staff->people_id, $staff->Name), $office, $staff->Phone);
}
$data['title'] = 'Search Result';
$data['table'] = $this->table->generate();
}
$this->load->view('search_staff', $data);
}
}
视图:
pagination类创建的链接不包含“search_by”和“keyword”条件,而且它们肯定不会出现在post变量中,因为当您单击页面链接时,您正在执行HTTP GET 您需要考虑将条件存储在类似用户会话的地方,以便可以对每个页面请求重复使用,或者考虑使用CodeIgniter 3中的分页库,该库支持将两个搜索词等项添加为查询字符串参数,然后您还可以检查这些参数 可以找到有关CodeIgniter 3分页类的详细信息 我自己没有尝试过,但您也应该能够修改分页链接的URL,使其为:
site_url('user/staffsearch/') . $search_by .'/' . $keyword;
然后,您可以读取各个URL段以检索搜索词
<div>
<h2><?php if (isset($title)) { echo $title;} ?></h2>
<?php if (isset($table)) { echo $table; } ?>
<?php if (isset($pagination)) { echo $pagination;} ?>
</div>
site_url('user/staffsearch/') . $search_by .'/' . $keyword;