用户搜索后的Codeigniter清除会话

时间:2013-06-08 20:47:24

标签: php codeigniter pagination session-variables

我正在使用搜索功能,该功能对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');   
        }
}   

我非常感谢一些帮助!

1 个答案:

答案 0 :(得分:0)

在页脚之后查看: -

$this->session->unset_userdata('searchChal');