2014-10-19 79 views
0

我有一個codeigniter搜索表單,其中包含一個下拉列表(Car)和一個複選框數組(Car types)。我正在使用POST方法從數據庫獲取值,但是發佈方法與分頁衝突,所以我決定使用GET方法。但是現在我的'if'聲明不起作用,它會返回我'其他'情況(即'search_nok'頁面,並顯示一條消息「請選擇您的搜索選項」)。你能檢查我的代碼,並幫我找到錯誤。使用get方法的Codeigniter分頁搜索結果

這裏是我的控制器

public function search($offset = 0) { 
       $limit = 5; 

       $this->load->library('form_validation'); 
       $this->load->model('model_x'); 

       $this->form_validation->set_rules('car', 'Car','required'); 
       $this->form_validation->set_rules('types', 'Car Type','required'); 

       if($this->form_validation->run()) { 

     $car= $this->input->get('car'); 
     $types = $this->input->get('types'); 

     $this->load->library('pagination'); 

     $config['base_url'] = 'http://localhost/abc/cont/search/'; 

     // 'http://localhost/abc' is my base url 

     $config['total_rows'] = 14; 
     $config['per_page'] = 5; 

     $data['pagination'] = $this->pagination->initialize($config); 

       if ($this->model_x->did_search($car, $types, $limit, $offset)){ 

       $data["results"] = $this->model_x->did_search($car, $types, $limit, $offset); 
       $this->load->view("search_ok",$data);   
       }   
       } 
       else 
       { 
       $data['message'] = 'Please select your options.'; 

       $this->load->view("search_nok",$data);   
       }    
      } 
+0

可能重複:可以驗證GET查詢字符串?(http://stackoverflow.com/questions/10883949/codeigniter-validation-possible-to-validate-get-query-strings ) – Alex 2014-10-19 10:57:23

+0

不,這是一個不同的問題 – EducateYourself 2014-10-19 11:06:56

+0

同樣的問題。您正試圖驗證通過'$ _GET'收到的表單。 SO上至少有3個相同的問題。 – Alex 2014-10-19 11:09:12

回答

1

這是因爲在CodeIgniter驗證類不檢查$_GET參數,並試圖驗證POST領域和發現沒有cartypes

若要速戰速決補充這一驗證$_GET參數您發送(因爲你沒有POST),你可以設置POST陣列是相同的,因此GET參數傳遞給驗證類。

$_POST = $_GET; 

這應該是驗證運行前:

$_POST = $_GET; 
$this->form_validation->set_rules('car', 'Car','required'); 
$this->form_validation->set_rules('types', 'Car Type','required'); 

if($this->form_validation->run()) { 
    // .... 
} 

UPDATE

爲了保持[笨驗證的search parameters across pages

+0

感謝您的幫助。對此,我真的非常感激。我只是添加了$ _POST = $ _GET;現在代碼的工作方式與以前完全一樣,即它正確顯示第一頁,但當我點擊第二頁時,搜索選項正在從URL中消失,並且我得到'else'語句 – EducateYourself 2014-10-19 10:20:33

+0

這意味着在您的視圖中,您的鏈接確實不包括您當前所在的偏移量或頁面,因此當再次調用控制器時,它可以計算「下一個」頁面。不是這樣,或者如果你的觀點是這樣的,那麼問題出在你的'$ this-> model_x-> did_search'上,當第一次加載結果的時候,沒有把右邊的'data'發送到視圖 – Kypros 2014-10-19 10:26:56

+0

這個url是:** http:// localhost/abc/cont/search?country = Mazda&types%5B%5D = sedan&filterSearch = Search **嘗試加載第二頁時,網址爲http:// localhost/abc/cont/search/5 **我想知道爲什麼汽車和類型沒有包含在下一頁的url中 – EducateYourself 2014-10-19 10:49:22