[英]how to write select query using if condition,And condition and where condition in codeigniter
[英]How to apply filter in codeigniter in 'where' condition using 'if' condition?
我正在尝试使用codeigniter应用过滤器,但无法正常工作。 它适用于单个数组记录,但是当我发送多个逗号分隔的源参数时,它不会过滤。
这是我在模型中的代码。
$user_id = $this->input->post('user_id');
$source = $this->input->post('source');
$source = explode(',', $source);
$radius = $this->input->post('radius');
$this->db->select('post.*, source.*, user.firstname, user.lastname');
$this->db->from('post');
$this->db->join('user', 'user.id = post.cop_id');
$this->db->join('source', 'post.source_id = source.id');
$this->db->where('post.cop_id', $user_id);
if (in_array("1", $source)) {
$this->db->where('post.source_id', 1);
}
if (in_array("2", $source)) {
$this->db->where('post.source_id', 2);
}
if (in_array("3", $source)) {
$this->db->where('post.source_id', 3);
}
if (in_array("4", $source)) {
$this->db->where('post.source_id',4);
}
if (!empty($radius)) {
$this->db->where('post.post_radius', $radius);
}
$this->db->where('post.source_id !=', 4);
$query = $this->db->get();
return $query->result();
使用where_in
$this->db->select('post.*, source.*, user.firstname, user.lastname');
$this->db->from('post');
$this->db->join('user', 'user.id = post.cop_id');
$this->db->join('source', 'post.source_id = source.id');
$this->db->where('post.cop_id', $user_id);
$this->db->where_in('post.source_id', $source);// it will produse IN ('1', '2', '3')
$this->db->where('post.post_radius', $radius);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.