您好我在CodeIgniter中开发了一个网站。
在我的一个页面中,我在搜索表单后使用了CodeIgniter的分页。
在这种情况下,我将$_POST
传递的搜索值存储到我的会话中,因为如果我有更多结果,则单击下一页搜索以获取搜索值。
但是当我更改页面时,我希望返回索引,并在返回到我的搜索表单页面后,会话已经创建并使用会话值进行查询。当我更改页面时,如何销毁或取消设置会话的价值?这可能吗?
在我的模型函数中,我检查会话值是否与0不同并且存在,如果为true,则使用会话值进行查询。
这是我的控制器(国家是存储到会话中的值)
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();
}
答案 0 :(得分:51)
回答你的问题:
如何销毁或取消会话的价值?
我可以帮助你:
$this->session->unset_userdata('some_name');
对于多个数据,您可以:
$array_items = array('username' => '', 'email' => '');
$this->session->unset_userdata($array_items);
并销毁会话:
$this->session->sess_destroy();
现在,对于页面更改部分(在我的脑海中):
您可以将 paginator 的 config“anchor_class”设置为您想要的 classname 。
之后只需使用 jquery onclick检查该类,它将向控制器函数发送一个将取消设置用户会话的函数。
答案 1 :(得分:6)
您应该使用set_flashdata。
而不是使用set_userdata根据CI用户指南:
CodeIgniter支持“flashdata”或仅可用于下一个服务器请求的会话数据,然后自动清除。这些非常有用,通常用于信息或状态消息(例如:“记录2已删除”)。
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
答案 2 :(得分:1)
:has
答案 3 :(得分:1)
我使用旧的PHP方式。它取消设置所有会话变量,并且不需要在数组中指定每个变量。取消设置变量后,我们破坏了会话。
session_unset();
session_destroy();
答案 4 :(得分:0)
$this->session->unset_userdata('session_value');
答案 5 :(得分:0)
我使用旧的PHP方式。它取消设置所有会话变量,并且不需要在数组中指定每个变量。取消设置变量后,我们销毁了会话