我有这样的模型函数:
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语句将被执行。
如果列tb_lqs083hdr.product_type = 'Finished Product'
,则第二个if语句将被执行。
这两个条件看起来彼此相似,但在我的表中,两个条件都有不同的值。
我还在评估我的查询。所以任何帮助都会受到赞赏。
感谢。
答案 0 :(得分: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的参数,否则你的代码中就没有意义了。
我希望有所帮助,
答案 1 :(得分: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()
}