嗨,我有一个在 CodeIgniter 中开发的网站。
在我的一个页面中,我在搜索表单后使用 CodeIgniter 的分页。
在这种情况下,我将传递的搜索值存储到我的会话中,$_POST
因为如果我有更多结果单击下一页,则搜索会保留搜索值。
但是,例如,当我更改页面时,我想返回索引,并且在返回我的搜索表单页面后,会话已经创建并使用会话的值进行查询。更改页面时如何销毁或取消设置会话的值?这可能吗?
在我的模型函数中,我检查会话值是否不同于 0 并且存在,如果为真,我使用会话值进行查询。
这是我的控制器(国家是存储到会话中的值)
public function region_list(){
$this->load->model('backend/Nation_model');
$this->load->library("pagination");
if($_POST)
{
if (isset($_POST['ricerca'])){
$nation = $this->input->post('nation');
if(strlen($nation) > 0){
$this->session->set_userdata('nation',$nation);
}
$config = array();
$config["base_url"] = base_url() . "index.php/backend/region/region_list";
$config["total_rows"] = $this->Region_model->countRegionSearch();
$config["per_page"] = 10;
$config["uri_segment"] = 4;
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data["regionlist"] = $this->Region_model->regionSearch($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$data["nationlist"] = $this->Nation_model->nationList();
$this->load->view('backend/region_list_view',$data);
}
}
else
{
$config = array();
$config["base_url"] = base_url() . "index.php/backend/region/region_list";
$config["total_rows"] = $this->Region_model->countRegion();
$config["per_page"] = 10;
$config["uri_segment"] = 4;
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data["links"] = $this->pagination->create_links();
$data["regionlist"] = $this->Region_model->regionList($config["per_page"], $page);
$data["nationlist"] = $this->Nation_model->nationList();
$this->load->view('backend/region_list_view',$data);
}
}
这是我要搜索的模型:
function regionList($limit=null, $start=null) {
$nation_id = $this->session->userdata('nation');
if ($this->session->userdata('language')=="it")
$this->db->select('region.id, region.name_it as name,nation.id as nation_id, nation.name_it as nation_name');
if ($this->session->userdata('language')=="en")
$this->db->select('region.id, region.name_en as name,nation.id as nation_id, nation.name_en as nation_name');
$this->db->from('region');
$this->db->join('nation', 'region.nation_id = nation.id','left');
if((isset($nation_id))&&($nation_id!=0))
$this->db->where('region.nation_id', $nation_id);
$this->db->order_by("name", "asc");
$this->db->limit($limit, $start);
$query = $this->db->get();
$region = array();
foreach ($query->result() as $row)
array_push($region, $row);
return $region;
}
function countRegion() {
$nation_id = $this->session->userdata('nation');
if ($this->session->userdata('language')=="it")
$this->db->select('region.id, region.name_it as name,nation.id as nation_id, nation.name_it as nation_name');
if ($this->session->userdata('language')=="en")
$this->db->select('region.id, region.name_en as name,nation.id as nation_id, nation.name_en as nation_name');
$this->db->from('region');
$this->db->join('nation', 'region.nation_id = nation.id','left');
if((isset($nation_id))&&($nation_id!=0))
$this->db->where('region.nation_id', $nation_id);
$this->db->order_by("name", "asc");
$query = $this->db->get();
return $query->num_rows();
}
public function regionSearch($limit=null, $start=null){
$nation_id = $this->session->userdata('nation');
if ($this->session->userdata('language')=="it"){
$this->db->select('*,region.id, region.name_it as name,nation.id as nation_id, nation.name_it as nation_name');
if($this->input->post('name')!="")
$this->db->where('region.name_it LIKE "%'.$this->input->post('name').'%"');
}
if ($this->session->userdata('language')=="en"){
$this->db->select('*,region.id, region.name_en as name,nation.id as nation_id, nation.name_en as nation_name');
if($this->input->post('name')!="")
$this->db->where('region.name_en LIKE "%'.$this->input->post('name').'%"');
}
$this->db->from('region');
$this->db->join('nation', 'region.nation_id = nation.id','left');
if((isset($nation_id))&&($nation_id!=0))
$this->db->where('region.nation_id', $nation_id);
$this->db->order_by("name", "asc");
$this->db->limit($limit, $start);
$query = $this->db->get();
$region = array();
foreach ($query->result() as $row)
array_push($region, $row);
return $region;
}
public function countRegionSearch(){
$nation_id = $this->session->userdata('nation');
if ($this->session->userdata('language')=="it"){
$this->db->select('*,region.id, region.name_it as name,nation.id as nation_id, nation.name_it as nation_name');
if($this->input->post('name')!="")
$this->db->where('region.name_it LIKE "%'.$this->input->post('name').'%"');
}
if ($this->session->userdata('language')=="en"){
$this->db->select('*,region.id, region.name_en as name,nation.id as nation_id, nation.name_en as nation_name');
if($this->input->post('name')!="")
$this->db->where('region.name_en LIKE "%'.$this->input->post('name').'%"');
}
$this->db->from('region');
$this->db->join('nation', 'region.nation_id = nation.id','left');
if((isset($nation_id))&&($nation_id!=0))
$this->db->where('region.nation_id', $nation_id);
$this->db->order_by("name", "asc");
$query = $this->db->get();
return $query->num_rows();
}