Codeigniter's活动记录IF语句


Codeigniter's Active Record IF Statement

我有一个这样的模型函数:

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语句总是在查询执行中执行,我希望第二个if语句也正在执行。

我有一个查询条件:
  • if列tb_lqs083hdr.product_type = 'Finished Product Coconut Cream',则首先执行if语句

  • else if列tb_lqs083hdr.product_type = 'Finished Product'则执行第二个if语句

这两个条件看起来很相似,但在我的表中,这两个条件的值不同。

我仍然在评估我的查询。因此,任何帮助都将不胜感激。

谢谢。

如果条件只是设置数据库调用,则不能使用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的参数,否则在你的代码中没有意义。

我希望这对你有帮助,

也许是这样的?

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()
}