Codeigniter:类似于具有空post数据的查询



我使用or_like通过表单查询数据库中已发布的值。如果代码下方的张贴字段均为空,则工作

$tbl_name= 'tbl_member_'.$this->session->userdata('userid');
$this->db->select('name,email,phone');
$this->db->from($tbl_name);
$this->db->group_start();
$this->db->like('name', $this->input->post('name'));
$this->db->or_like('phone',$this->input->post('phonenumber'));
$package= $this->input->post('package');
$this->db->or_like('body_weight', $this->input->post('body_weight'));
$this->db->group_end();
$query = $this->db->get();
return  $query->result());

否则它会打印所有记录,请指导我如何解决空值被忽略的问题,我试图将我的查询包装在php块中,但它不起作用

$tbl_name= 'tbl_member_'.$this->session->userdata('userid');
$this->db->select('name,email,phone');
$this->db->from($tbl_name);
$this->db->group_start();
if($this->input->post('name') && $this->input->post('name')!='' ){
$this->db->like('name', $this->input->post('name'));
}
if($this->input->post('phonenumber') && $this->input->post('phonenumber')!='' ){
$this->db->or_like('phone',$this->input->post('phonenumber'));
}
if($this->input->post('package') && $this->input->post('package')!='' ){
$package= $this->input->post('package');
}
if($this->input->post('body_weight') && $this->input->post('body_weight')!='' ) {
$this->db->or_like('body_weight', $this->input->post('body_weight'));
}
if($this->input->post('blood_group') && $this->input->post('blood_group')!='' ){
$this->db->or_like('blood_group',$this->input->post('blood_group'));
}
if($this->input->post('gender') && $this->input->post('gender')!='' ){
$this->db->or_like('gender',$this->input->post('gender'));
}
$this->db->group_end();
$query = $this->db->get();

感谢

编辑

我用下面的代码来排序我的问题,它运行得很好:

if($this->input->post()){
$form_values= array(
'name'         =>  $this->input->post('name'),
'phone'         =>  $this->input->post('phonenumber'),
'gender'        =>  $this->input->post('gender'),
'package'       =>  $this->input->post('package'),
'body_weight'   =>  $this->input->post('body_weight'),
'blood_group'   => $this->input->post('blood_group'),
);
$data['fields']= $form_values;
$form_values=array_filter($form_values);
if (!empty($form_values)){
$tbl_name= 'tbl_member_'.$this->session->userdata('userid');
$this->db->select('id,name,phone,blood_group,gender,body_weight,package');
$this->db->from($tbl_name);
$this->db->like($form_values);
$data['members'] = $this->db->get()->result();
}

您可以尝试以下操作吗:

$tbl_name= 'tbl_member_'.$this->session->userdata('userid');
$this->db->select('name,email,phone');
$this->db->from($tbl_name);
$this->db->group_start();
if(!empty($this->input->post('name'))){
$this->db->or_like('name', $this->input->post('name'));
}
if(!empty($this->input->post('phonenumber'))){
$this->db->or_like('phone',$this->input->post('phonenumber'));
}
if(!empty($this->input->post('package'))){
$package= $this->input->post('package');
}
if(!empty($this->input->post('body_weight'))) {
$this->db->or_like('body_weight', $this->input->post('body_weight'));
}
if(!empty($this->input->post('blood_group'))){
$this->db->or_like('blood_group',$this->input->post('blood_group'));
}
if(!empty($this->input->post('gender'))){
$this->db->or_like('gender',$this->input->post('gender'));
}
$this->db->group_end();
$query = $this->db->get();

最新更新