2016-08-08 35 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() 
} 
相关问题