我正在使用对 codeigniter 中的页面进行分页的搜索功能。因此,我将搜索词保存在会话中,因此当您切换页面时,该词仍然可用。不过,我有一个小问题,它会保存您输入的所有内容。
我知道我需要使用这个清除会话
$this->session->unset_userdata('searchChal');
但我只是不确定把它放在哪里。我尝试在函数的开头和结尾放置,但它清除会话太快,单击下一页时会话不再存在。
当用户输入另一个值时,有什么方法可以清除会话?还是我只是错过了什么?
这是我的功能
public function searchChallenges(){
//This function allows you to search through all the challenges
$config["base_url"] = base_url()."index.php/story/searchChallenges";
$config["total_rows"] = $this->data_model->searchChal_count();
$config["per_page"] = 1;
$config["uri_segment"] = 3;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = round($choice);
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$searchTerm = $this->input->post("searchC", TRUE);
if($searchTerm) {
$searchChal= $this->input->post("searchC", TRUE);
$this->session->set_userdata('searchChal', $searchChal);
}elseif($this->session->userdata('searchChal')){
$this->search = $this->session->userdata('searchChal');
}else{
$searchTerm ="";
}
if (empty($this->user)) {
$this->load->view('header_view', array(
'cSearch' => $this->session->userdata('searchChal'),
));
$story = $this->story_model->searchChal($config['per_page'], $page);
if (! isset($story[0]->challenge_id)){
$this->load->view('noSearchChal_view');
}else{
$this->load->view('challenge_view', array(
'query' => $this->story_model->searchChal($config['per_page'], $page),
'links' => $this->pagination->create_links(),
'searchTerm' => $this->session->userdata('searchChal'),
));
}
$this->load->view('footer_view');
}else{
$this->load->view('header_view',array(
'user' => $this->users_model->getUser($this->user->user_id),
'cSearch' => $this->input->post("searchC"),
));
$story = $this->story_model->searchChal($config['per_page'], $page);
if (! isset($story[0]->challenge_id)){
$this->load->view('noSearchChal_view');
}else{
$this->load->view('challenge_view', array(
'query' => $this->story_model->searchChal($config['per_page'], $page),
'links' => $this->pagination->create_links(),
'admin' => $this->admin_model->adminSearchChal(),
'searchTerm' => $searchTerm,
));
}
$this->load->view('footer_view');
}
}
我将不胜感激一些帮助!