2015-10-01 37 views
0

下拉框根據用戶分配給的客戶動態填充。問題是,當我要搜索的客戶,用戶被分配到(如果他們選擇觀看All客戶的選擇),我會做一個查詢,如:Where子句基於多個動態值,CodeIgniter

查詢1:

SELECT * FROM LotDispDashBoardDB.disp_dummy where (customer = 'DL1' or customer = 'BC1') and stage like 'T%' and lot_status like 'close%'; 

我不想做

查詢2:

SELECT * FROM LotDispDashBoardDB.disp_dummy where stage like 'T%' and lot_status like 'close%'; 

,因爲我希望用戶能夠只看到自己分配的客戶,我不想讓他們牛逼o查看所有可用的客戶。

下拉框只有一個固定值,即All,其餘部分是各個客戶的名稱。如果用戶選擇All意味着他想查看他的指定客戶All。在下面的控制器中,如果他選擇所有我會得到$all_customer陣列。如果不是,我只會讓客戶選中。

在模型中知道它是否全部,我檢測收到的值是否是一個數組。如果它是一個數組,我遍歷它使用CodeIgniter的活動記錄$this->db->or_where('customer', $customer);來執行查詢。 主要問題是查詢。如前所述,我嘗試去做如查詢1. 我怎樣才能放置括號並動態地編寫where子句?

控制器:

public function load_lot_table() 
{ 
    // Get customers from session 
    $customer_session = $this->session->userdata['logged_in']['customers']; 

    foreach($customer_session as $cust_object) 
    { 
     $all_customer[] = $cust_object->customer; 
    } 

    // Get form values 
    if($this->input->get()) 
    { 
     $selected_customer = $this->input->get('cust_drop_down'); 
     $stage = $this->input->get('area_drop_down'); 
     $lot_status = $this->input->get('status_drop_down'); 
     $search_lot = ltrim($this->input->get('search_lot')); 

     if($selected_customer == 'all') 
     { 
      $customer = $all_customer; 
     } 
     else 
     { 
      $customer = $selected_customer; 
     } 
    } 
    else 
    { 
     // Default values when page is loaded because form is not submitted yet 
     $customer = $all_customer; 
     $stage = 'all'; 
     $lot_status = 'all'; 
     $search_lot = ''; 
    } 

    // Keys are the column names 
    $search = array(
    'customer' => $customer, 
    'stage' => $stage, 
    'lot_status' => $lot_status, 
    'search_lot' => $search_lot 
    ); 

    // Paginate retrieved lots 
    $results = $this->paginate_create_table($search); 

    /*** other codes ***/ 

} 

private function paginate_create_table($search = null) 
{ 
    if(is_array($search['customer'])) 
    { 
     $customer = 'all'; 
    } 
    else 
    { 
     $customer = $search['customer']; 
    } 

    // Set pagination configuration 
    $config = array(); 
    $config['base_url'] = base_url()."index.php/Home/on_hold_lot"; 
    $config['suffix'] = '?cust_drop_down='.$customer.'&area_drop_down='.$search['stage'].'&status_drop_down='.$search['lot_status'].'&search_lot='.$search['search_lot']; 
    $config['first_url'] = $config['base_url'].$config['suffix']; 
    $config['total_rows'] = $this->home_model->fetch_lots('rows', $search); 
    $config['per_page'] = 10; 
    $config['uri_segment'] = 3; 

    /*** Other codes ****/ 
} 

型號:

public function fetch_lots($return, $search = null, $limit = null, $start = 0) 
{ 
    if($limit != null) 
    { 
     $this->db->limit($limit, $start); 
    } 

    if($search != null) 
    { 
     if($search['customer'] != 'all' && !is_array($search['customer'])) 
     { 
      $this->db->where('customer', $search['customer']); 
     } 
     elseif(is_array($search['customer'])) 
     { 
      foreach($search['customer'] as $customer) 
      { 
       $this->db->or_where('customer', $customer); 
      } 
     } 

     if($search['stage'] != 'all') 
     { 
      $this->db->like('stage', $search['stage'], 'after'); 
     } 

     if($search['lot_status'] != 'all') 
     { 
      $this->db->like('lot_status', $search['lot_status'], 'after'); 
     } 

     if($search['search_lot'] != '') 
     { 
      $this->db->where('utac_lot', $search['search_lot']); 
     } 
    } 
    /*** Other codes ***/ 
} 

回答

0

笨允許你pass a string$this->db->where()

實施例:

$where = "(this = $that)"; 
$this->db->where($where); 

在循環訪問$search['customer']時,可以創建一個字符串用作where查詢。

if($search['customer'] != 'all' && !is_array($search['customer'])) 
    { 
     $this->db->where('customer', $search['customer']); 
    } 
    elseif(is_array($search['customer'])) 
    { 
     for($i = 0, $i <= count($search['customer'], $i++) 
     { 
      // Use a FOR instead of FOREACH so we can count the loops. 
      // 'WHERE' will be used on the first loop, 
      // subsequent loops use 'OR'. 
      if($i == 0) 
      { 
       $where = "(WHERE customer = $search['customer'][$i] "; 
      } else { 
       $where .= " OR customer` = $search['customer'][$i] "; 
      } 
      if($i == count($search['customer'])) 
      { 
       $where .= ") "; 
      } 
     } 
     $this->db->where($where); 
    } 
//... etc.. 
} 
+0

謝謝,但我想我找到了一個更簡單的解決方案。將張貼,讓我知道你的想法。 – hzq

0

這是我想出的解決方案。

// If user selects a customer 
if($search['customer'] != 'all' && !is_array($search['customer'])) 
{ 
    $this->db->where('customer', $search['customer']); 
} 
// If user selects ALL 
elseif(is_array($search['customer'])) 
{ 
    foreach($search['customer'] as $customer) 
    { 
     $all_customer[] = "customer = '$customer'"; 
    } 

    // Example query would form: (customer = 'ABC' OR customer = 'BCD') 
    $where_customer = "("; 
    $where_customer .= implode(' OR ', $all_customer); 
    $where_customer .= ")"; 

    $this->db->where($where_customer); 
}