2016-08-08 34 views
1

我有一個這樣的模型功能:CodeIgniter的Active Record的IF語句

function get_dtsample083($dt_date_production,$dt_filler) { 
    $this->db1->select('detail_id_sample as detail_id083, detail_id as detail_id_mic, detail_id_sample087_096, headerid_sample, sampling_time, sample_code, product_id, temp, filler, ph, csp_la_35, csp_la_55, tab, operator, remark, rough_mic'); 
    $this->db1->from('tb_lqs083dtl'); 
    $this->db1->join('tb_lqs083hdr', 'tb_lqs083hdr.headerid = tb_lqs083dtl.headerid'); 
    $this->db1->where('tb_lqs083hdr.date_production',$dt_date_production); 
    $this->db1->where('tb_lqs083hdr.filler',$dt_filler); 

    $try1 = 'Finished Product Coconut Cream'; 
    $try2 = 'Finished Product'; 

    if($this->db1->where('tb_lqs083hdr.product_type',$try1)) { 
     $this->db1->where('tb_lqs083hdr.product_type',$try1); 
     $this->db1->where('tb_lqs083dtl.stdtl','1'); 
     $this->db1->order_by("tb_lqs083dtl.headerid_sample", "asc"); 
     $this->db1->order_by("tb_lqs083dtl.detail_id_sample087_096", "asc"); 
     $this->db1->order_by("tb_lqs083hdr.temp", "asc");   
    } elseif($this->db1->where('tb_lqs083hdr.product_type !=',$try1)) { 
     $this->db1->where('tb_lqs083hdr.product_type',$try2); 
     $this->db1->where('tb_lqs083hdr.product_name','Coconut Cream'); 
     $this->db1->where('tb_lqs083dtl.stdtl','1'); 
     $this->db1->order_by("tb_lqs083dtl.headerid_sample", "asc"); 
     $this->db1->order_by("tb_lqs083dtl.detail_id_sample087_096", "asc"); 
     $this->db1->order_by("tb_lqs083hdr.temp", "asc"); 
    } else {} 

    $query = $this->db1->get(); 
    echo $this->db1->last_query(); 
    if ($query->num_rows() > 0) { 
     return $query->result(); 
    } 
} 

第一,如果總是在執行查詢執行的語句,我希望第二個if語句也正在執行。

我有查詢條件:

  • 如果列tb_lqs083hdr.product_type = 'Finished Product Coconut Cream'然後第一個if語句將被執行。

  • else if if tb_lqs083hdr.product_type = 'Finished Product' then second if statement will be executed。

兩個條件似乎彼此相似,但在我的表中,兩個條件都有不同的值。

我仍在評估我的查詢。所以任何幫助,將不勝感激。

謝謝。

回答

1

如果條件只是設置數據庫調用,則不能執行if語句。由於where語句總是被設置,第一個條件總是被滿足。

你可以用很多方式解決這個問題,但是你需要解決你的代碼的邏輯問題。

您可以運行查詢以查明'tb_lqs083hdr.product_type'是否找到try1或不。然後在你的第二個查詢的if語句中使用該查詢的結果。

您可以使用查詢生成器條件語句:

$this->db->select('*')->from('my_table') 
     ->group_start() 
      ->where('a', 'a') 
      ->or_group_start() 
       ->where('b', 'b') 
       ->where('c', 'c') 
      ->group_end() 
     ->group_end() 
     ->where('d', 'd') 
    ->get(); 

http://www.codeigniter.com/user_guide/database/query_builder.html#query-grouping

然而,在PHP中,如果你正在寫if語句,它需要一個有可能是TRUE或FALSE的參數,否則在你的代碼中沒有意義。

我希望有幫助,

+0

好吧,我會嘗試使用你的建議,希望它能工作 – metaphor

+0

也許這樣的事情?見下面的答案: – PaulD

0

也許這樣的事情?

function get_dtsample083($dt_date_production,$dt_filler) { 
    $try1 = 'Finished Product Coconut Cream'; 
    $try2 = 'Finished Product'; 

    $query = $this->db1->select('detail_id_sample as detail_id083, detail_id as detail_id_mic, detail_id_sample087_096, headerid_sample, sampling_time, sample_code, product_id, temp, filler, ph, csp_la_35, csp_la_55, tab, operator, remark, rough_mic') 
     ->from('tb_lqs083dtl') 
     ->join('tb_lqs083hdr', 'tb_lqs083hdr.headerid = tb_lqs083dtl.headerid') 
     ->where('tb_lqs083hdr.date_production',$dt_date_production) 
     ->where('tb_lqs083hdr.filler',$dt_filler) 
     ->group_start() 
      ->where('tb_lqs083hdr.product_type',$try1) 
      ->or_group_start() 
       ->where('tb_lqs083hdr.product_type',$try2) 
      ->group_end() 
     ->group_end() 
     ->where('tb_lqs083dtl.stdtl','1') 
     ->order_by("tb_lqs083dtl.headerid_sample", "asc") 
     ->order_by("tb_lqs083dtl.detail_id_sample087_096", "asc") 
     ->order_by("tb_lqs083hdr.temp", "asc") 
    ->get() 
} 
相關問題