【问题标题】:Codeigniter's Active Record IF StatementCodeigniter Active Record IF 语句
【发布时间】:2016-08-08 04:40:00
【问题描述】:

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

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 语句也正在执行。

我有一个查询条件:

  • 如果列tb_lqs083hdr.product_type = 'Finished Product Coconut Cream',则将执行第一个if语句。

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

这两个条件似乎彼此相似,但在我的表格中,两个条件的值不同。

我仍在评估我的查询。因此,我们将不胜感激。

谢谢。

【问题讨论】:

    标签: php database postgresql codeigniter


    【解决方案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 的参数,否则在你的代码中使用它是没有意义的。

    希望对你有帮助

    【讨论】:

    • 可能是这样的?请参阅下面的答案:
    【解决方案2】:

    大概是这样的吧?

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

    【讨论】:

      猜你喜欢
      • 2016-02-25
      • 2013-04-11
      • 2014-12-24
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多