2016-08-08 10 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(); 
    } 
} 

最初の文も実行されている場合、私は2番目をしたいです。

  • が列tb_lqs083hdr.product_type = 'Finished Product Coconut Cream'は、最初の文が実行される場合場合:

    は、私は、クエリの条件を持っています。

  • elseコラムtb_lqs083hdr.product_type = 'Finished Product'の場合は、2番目のif文が実行されます。

どちらの条件も互いに類似しているようですが、私の表では両方の条件に異なる値があります。

私はまだクエリを評価しています。だから、どんな助けもありがとう。

ありがとうございました。

答えて

1

条件文がデータベース呼び出しを設定するだけのif文は実行できません。 whereステートメントは常に設定されているので、最初の条件は常に満たされます。

これはさまざまな方法で解決できますが、コードのロジックに対処する必要があります。

「tb_lqs083hdr.product_type」がtry1を検出したかどうかを確認するためにクエリを実行できます。次に、2番目のクエリのif文でそのクエリの結果を使用します。あなたはTRUEまたはFALSE可能性が引数を持っていることが必要if文作成している場合は、PHPで、

$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

しかし:

クエリビルダで条件文を使用することができますそうでなければ、あなたのコードにそれがあることには意味がありません。

+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() 
} 
関連する問題