2015-10-16 84 views
1

在下面的代碼中,我得到了第三個if-else語句的空數組。我想要的是從用戶那裏得到一系列的日期並相應地顯示錶中的數據。我的第一個if-else語句返回所需結果,但第二個語句返回空數組。在codeigniter項目中獲取一個空數組

控制器:

public function bookings() 
    { 
    if($this->userlib->isLoggedIn()) 
    { 
     if($this->userlib->isAdmin()) 
     { 
      $this->load->view('admin_search_booking'); 
      $date_posted_from = $this->input->post('date_posted_from'); 
      $date_posted_till = $this->input->post('date_posted_till'); 
      $date_posted_on = $this->input->post('date_posted_on'); 
      if(is_null($date_posted_from) && is_null($date_posted_till) && is_null($date_posted_on)) 
      { 
       $total_trails = $this->admin_panel_model->total_trail();  
       var_dump($total_trails); 
      } 
      elseif(!is_null($date_posted_on)) 
      { 
       $total_trails = $this->admin_panel_model->filter_on_date($date_posted_on); 
       var_dump($total_trails); 
      } 
      elseif(!is_null($date_posted_from) && !is_null($date_posted_till)) 
      { 
       $total_trails = $this->admin_panel_model->filter_by_date($date_posted_from, $date_posted_till); 
       var_dump($total_trails); 
      } 
     } 
    } 
    else 
    { 
     echo "User not Logged In"; 
    } 
} 

型號filter_by_date: -

public function filter_by_date($date_posted_from, $date_posted_till) 
{ 
    $query = $this->db->select('*') 
         ->where('date >=', $date_posted_from) 
         ->where('date <=', $date_posted_till) 
         ->get($this->search); 
    return $query->result(); 
} 

isLoggedIn: -

public function isLoggedIn() 
{ 
    if($this->ci->session->email) 
     return true; 
    else 
     return false; 
} 
+0

嘗試'echo'您的查詢,並檢查它的Sql –

+0

查詢中工作是工作的罰款,檢查,居然當我刪除第一ifelse,其他ifelse工作得很好。 –

+0

發表你的'isLoggedIn'功能 –

回答

0

更改模型的功能這樣

public function filter_by_date($date_posted_from="", $date_posted_till="") 
{ 
    $query = $this->db->select('*'); 
    if($date_posted_from) 
     $this->db->where('date >=', $date_posted_from); 
    if($date_posted_till) 
     $this->db->where('date <=', $date_posted_till); 
    $this->db->get($this->search); 
    return $query->result(); 
} 
+0

仍然不能正常工作 –

+0

打印最後一個查詢並在phpmyadmin運行查詢並檢查那裏。 –

0
//This may help you put your table name in your select statement and update where clause 
public function filter_by_date($date_posted_from, $date_posted_till){ 
     $date_posted_from='2015-10-10'; 
     $date_posted_till='2015-10-16'; 
     $query = $this->db->select('*')->from('Your_Table_Name') 
          ->where("date >= '".$date_posted_from."'") 
          ->where("date <= '".$date_posted_till."'") 
          ->get($this->search); 
     return $query->result(); 
    } 
+0

您正在將值初始化爲$ date_posted_from和$ date_posted_till,但我希望僅從控制器文件中檢索它們。 –

+0

僅傳遞控制器中的值並調用您的方法我已初始化變量,例如。評論那些初始化的變量。 –

+0

像這樣: $ admin = new admin(); //創建模型文件的對象 $ date_posted_from ='2015-10-10'; $ date_posted_till ='2015-10-16'; $ list = $ admin-> filter_by_date($ date_posted_from,$ date_posted_till); –

0

使用$這個 - > DB-> last_query();在filter_by_date()之後並檢查實際正在執行的查詢。還要確保在您搜索的日期範圍內有記錄。 或者乾脆使用簡單的查詢,

$Q = $this->db->query("select * from table_name where date >='".$date_posted_from."' and date <='".$date_posted_to."'"); 
return $Q->result_array();