こんにちは私はCodeIgniterでサイトを開発しています。
私のページの1つで、検索フォームの後に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();
}