Php 查询内的CodeIgniter条件语句
这是我的代码的简化版本:Php 查询内的CodeIgniter条件语句,php,codeigniter-2,Php,Codeigniter 2,这是我的代码的简化版本: $this->db->select('column1, column2, status, etc'); if ($search['option_1']){ $this->db->or_where('status', 15); $this->db->or_where('status', 20); } if ($search['option_2']){ $
$this->db->select('column1, column2, status, etc');
if ($search['option_1']){
$this->db->or_where('status', 15);
$this->db->or_where('status', 20);
}
if ($search['option_2']){
$this->db->or_where('status', 30);
$this->db->or_where('status', 40);
$this->db->or_where('status', 50);
}
if ($search['option_3']){
$this->db->or_where('status', 99);
}
$this->db->where('column', $search['value']);
etc..
在网页(搜索表单)上,除其他字段和选择框外,我有三个复选框:选项1到选项3。所有复选框都是可选的。每个复选框对应一个状态
复选框可以全部选中、部分选中或不选中
我认为这段代码的问题在于OR语句周围缺少括号,因此“中断”了查询。我是否必须:
if ($search['option_1']){
$this->db->where('(status = 15 OR status = 20)');
}
if ($search['option_1'] && $search['option_2']){
$this->db->where('(status = 15 OR status = 20 OR status = 30 OR status = 40 OR status = 50)');
}
etc...
还是有更聪明的方法
[更新]我一直在用这个,直到有人想出更好的方法:
if($search['option_1'] || $search['option_2'] || $search['option_3']) {
if ($search['option_1']){
$status = '(status = 15 OR status = 20)';
}
else if ($search['option_2']){
$status = '(status = 30 OR status = 40 OR status = 50)';
}
else if($search['option_3']) {
$status = 'hr_olas.k_VALU_Status = 99';
}
else if($search['option_1'] && $search['option_2']) {
$status = '(status = 15 OR status = 20 OR status = 30 OR status = 40 OR status = 50)';
}
else if($search['option_1'] && $search['option_3']) {
$status = '(status = 15 OR status = 20 OR status = 99)';
}
else if($search['option_2'] && $search['option_3']) {
$status = '(status = 30 OR status = 40 OR status = 50 OR status = 99)';
}
else if($search['option_1'] && $search['option_2'] && $search['option_3']) {
$status = '(status = 15 OR status = 20 OR status = 30 OR status = 40 OR status = 50 OR status = 99)';
}
$this->db->where($status);
}
好的方法是(在(15,20)中的状态)
但要调试您的问题,请打印查询以查看问题所在
echo $this->db->last_query();
假设如下:
if ($search['option_1']){
$this->db->select('column1, column2, status, etc');
$this->db->or_where('status', 15);
$this->db->or_where('status', 20);
$this->db->where('column', $search['value']);
etc
$query = $this->db->get();
}
if ($search['option_2']){
$this->db->select('column1, column2, status, etc');
$this->db->where('column', $search['value']);
$this->db->or_where('status', 30);
$this->db->or_where('status', 40);
$this->db->or_where('status', 50);
etc
$query = $this->db->get();
}
if ($search['option_3']){
$this->db->select('column1, column2, status, etc');
$this->db->where('column', $search['value']);
$this->db->or_where('status', 99);
etc
$query = $this->db->get();
}
希望,这会有帮助……好吧,从技术上讲,这个查询没有错。问题在于,OR语句与标准相结合,与我的预期不符。这是因为OR语句周围缺少括号。我重新阅读了我的问题,现在我发现不清楚复选框是否可以全部选中,部分选中或不选中。我会更新我的问题不幸的是,这也不起作用。在where和or_之间,查询中出现了一个在这种情况下不需要的或。
$this->db->select('column1, column2, status, etc');
$this->db->where('column', $search['value']);
if ($search['option_1']){
$this->db->or_where(array('status'=>'15','status'=>20));
}
if ($search['option_2']){
$this->db->or_where(array('status'=>'30','status'=>'40','status'=>'50'));
}
if ($search['option_3']){
$this->db->or_where(array('status'=>'99'));
}
$query = $this->db->get();