搜索过滤器无法与 Codeigniter 中的分页一起正常工作
Search filter not working properly with Pagination in Codeigniter
我是 PHP 和 C.I 的新手,我正在创建一个搜索过滤器,过滤器工作正常,直到我移动到下一个分页 link。当我在 First link 时,数据根据 filter/search 关键字显示,但是当我移动到下一个 link 时,一切都消失了(所有数据都显示在页面上)。
我搜索了很多并浏览了很多 links/tutorials 但没有找到 authentic/proper/specific 此类问题的答案。
我在 Stack Overflow 上找到了一个 Link 并关注它,但没有运气。
我的控制器代码:
public function URecords() {
$config = array();
$keyword = $this->input->post('search');
$this->session->set_flashdata('search',$keyword);
$config["base_url"] = base_url() . "master/URecords";
$config["total_rows"] = $this->bm->record_count($keyword);
$config['use_page_numbers'] =FALSE;
$config['cur_tag_open'] = '<a><b class="text-success">';
$config['cur_tag_close'] = '</b></a>';
$config["per_page"] =4;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["posts"] = $this->bm->fetch_countries($config["per_page"], $page,$keyword);
$data["links"] = $this->pagination->create_links();
$this->load->view('Admin/header');
$this->load->view('Admin/nav');
$this->load->view('Admin/sidebar');
$this->load->view('Admin/userrecord', $data);
$this->load->view('Admin/footer');
}
我的模型代码:
public function record_count($keyword) {
$this->db->like('Employee_Name',$keyword);
$this->db->from('dc_user');
return $this->db->count_all_results();
// return $this->db->count_all("dc_user");
}
public function fetch_countries($limit, $start,$keyword) {
$this->db->limit($limit, $start);
$this->db->like('Employee_Name',$keyword);
$query = $this->db->get_where("dc_user");
if(empty($query->result()))
{
//echo "No record found in data base";
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">No Record Found!</div>');
}
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
我的查看代码:
<div class="right_col" role="main">
<div class="">
<div class="page-title">
<div class="title_right">
<div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search">
<form action="<?php echo base_url();?>Master/URecords" method="post">
<div class="input-group">
<input type="text" class="form-control" id="search" name="search" value="<?php if(isset($_SESSION['search'])){echo $_SESSION['search'];}?>" placeholder="Search for...">
<span class="input-group-btn">
<input class="btn btn-default" type="submit" name="submit" id="submit" value="GO!">
</span>
</div>
</form>
</div>
</div>
</div>
<div class="clearfix"></div>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>All User's Record</h2>
<ul class="nav navbar-right panel_toolbox">
<li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Settings 1</a>
</li>
<li><a href="#">Settings 2</a>
</li>
</ul>
</li>
<li><a class="close-link"><i class="fa fa-close"></i></a>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="x_content">
<div class="table-responsive">
<?php if(isset($_SESSION['msg'])){?>
<span class="text-info col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<?php echo $this->session->flashdata('msg'); ?>
</span>
<?php } else {?>
<table class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th class="column-title">Employee Name </th>
<th class="column-title">Email </th>
<th class="column-title">Contact # </th>
<th class="column-title">Date Of Birth </th>
<th class="column-title">Designation </th>
<th class="column-title">Profile Picture </th>
</tr>
</thead>
<tbody>
<?php foreach($posts as $post) { ?>
<tr class="even pointer">
<td class=" "><?php echo $post->Employee_Name; ?></td>
<td class=" "><span class="text-info"><?php echo $post->Email; ?></span></td>
<td class=" "><?php echo $post->Contact; ?></td>
<td class=" "><?php echo $post->DOB; ?></td>
<td class=" "><?php echo $post->Designation; ?></td>
<td class=" "><img src="<?php echo base_url();?>profileimages/<?php echo $post->Profile_Image; ?>" alt="..." class="img-square profile_img" style="width: 200px !important; height: 100px !important;"> </td>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="text-center"><nav aria-label="Page navigation">
<ul class="pagination">
<li><?php echo $links; ?></li>
</ul>
</nav></div>
<?php }?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
主要问题:
问题是:当我在搜索字段中输入任何关键字时,它会在开始时显示正确的结果,但是当我移动到其他分页时 links 然后我失去了搜索结果。
我想要的:
当我通过分页 links 时,它将像在搜索开始时一样工作。
问题是您在浏览分页链接时没有检索关键字。
分页是常规的 <a>
,因此它不会发送任何 POST 数据
替换此行:
$keyword = $this->input->post('search');
$this->session->set_flashdata('search',$keyword);
有了这个:
$keyword = $this->input->post('search');
if ($keyword === null) $keyword = $this->session->userdata('search');
else $this->session->set_userdata('search',$keyword);
此代码将关键字保存到会话中,并检查关键字是否未由 POST 提供,然后从会话中检索关键字
我是 PHP 和 C.I 的新手,我正在创建一个搜索过滤器,过滤器工作正常,直到我移动到下一个分页 link。当我在 First link 时,数据根据 filter/search 关键字显示,但是当我移动到下一个 link 时,一切都消失了(所有数据都显示在页面上)。 我搜索了很多并浏览了很多 links/tutorials 但没有找到 authentic/proper/specific 此类问题的答案。 我在 Stack Overflow 上找到了一个 Link 并关注它,但没有运气。
我的控制器代码:
public function URecords() {
$config = array();
$keyword = $this->input->post('search');
$this->session->set_flashdata('search',$keyword);
$config["base_url"] = base_url() . "master/URecords";
$config["total_rows"] = $this->bm->record_count($keyword);
$config['use_page_numbers'] =FALSE;
$config['cur_tag_open'] = '<a><b class="text-success">';
$config['cur_tag_close'] = '</b></a>';
$config["per_page"] =4;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["posts"] = $this->bm->fetch_countries($config["per_page"], $page,$keyword);
$data["links"] = $this->pagination->create_links();
$this->load->view('Admin/header');
$this->load->view('Admin/nav');
$this->load->view('Admin/sidebar');
$this->load->view('Admin/userrecord', $data);
$this->load->view('Admin/footer');
}
我的模型代码:
public function record_count($keyword) {
$this->db->like('Employee_Name',$keyword);
$this->db->from('dc_user');
return $this->db->count_all_results();
// return $this->db->count_all("dc_user");
}
public function fetch_countries($limit, $start,$keyword) {
$this->db->limit($limit, $start);
$this->db->like('Employee_Name',$keyword);
$query = $this->db->get_where("dc_user");
if(empty($query->result()))
{
//echo "No record found in data base";
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">No Record Found!</div>');
}
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
我的查看代码:
<div class="right_col" role="main">
<div class="">
<div class="page-title">
<div class="title_right">
<div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search">
<form action="<?php echo base_url();?>Master/URecords" method="post">
<div class="input-group">
<input type="text" class="form-control" id="search" name="search" value="<?php if(isset($_SESSION['search'])){echo $_SESSION['search'];}?>" placeholder="Search for...">
<span class="input-group-btn">
<input class="btn btn-default" type="submit" name="submit" id="submit" value="GO!">
</span>
</div>
</form>
</div>
</div>
</div>
<div class="clearfix"></div>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>All User's Record</h2>
<ul class="nav navbar-right panel_toolbox">
<li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Settings 1</a>
</li>
<li><a href="#">Settings 2</a>
</li>
</ul>
</li>
<li><a class="close-link"><i class="fa fa-close"></i></a>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="x_content">
<div class="table-responsive">
<?php if(isset($_SESSION['msg'])){?>
<span class="text-info col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<?php echo $this->session->flashdata('msg'); ?>
</span>
<?php } else {?>
<table class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th class="column-title">Employee Name </th>
<th class="column-title">Email </th>
<th class="column-title">Contact # </th>
<th class="column-title">Date Of Birth </th>
<th class="column-title">Designation </th>
<th class="column-title">Profile Picture </th>
</tr>
</thead>
<tbody>
<?php foreach($posts as $post) { ?>
<tr class="even pointer">
<td class=" "><?php echo $post->Employee_Name; ?></td>
<td class=" "><span class="text-info"><?php echo $post->Email; ?></span></td>
<td class=" "><?php echo $post->Contact; ?></td>
<td class=" "><?php echo $post->DOB; ?></td>
<td class=" "><?php echo $post->Designation; ?></td>
<td class=" "><img src="<?php echo base_url();?>profileimages/<?php echo $post->Profile_Image; ?>" alt="..." class="img-square profile_img" style="width: 200px !important; height: 100px !important;"> </td>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="text-center"><nav aria-label="Page navigation">
<ul class="pagination">
<li><?php echo $links; ?></li>
</ul>
</nav></div>
<?php }?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
主要问题:
问题是:当我在搜索字段中输入任何关键字时,它会在开始时显示正确的结果,但是当我移动到其他分页时 links 然后我失去了搜索结果。
我想要的:
当我通过分页 links 时,它将像在搜索开始时一样工作。
问题是您在浏览分页链接时没有检索关键字。
分页是常规的 <a>
,因此它不会发送任何 POST 数据
替换此行:
$keyword = $this->input->post('search');
$this->session->set_flashdata('search',$keyword);
有了这个:
$keyword = $this->input->post('search');
if ($keyword === null) $keyword = $this->session->userdata('search');
else $this->session->set_userdata('search',$keyword);
此代码将关键字保存到会话中,并检查关键字是否未由 POST 提供,然后从会话中检索关键字