php - Codeigniter : Like query with empty post data
i am querying db against posted values through the form using or_like. If none of the posted field is empty below code works
$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());
else it prints all the records, please guide me on how can I fix my issue that empty value is ignored, i tried to wrap my query as below in php blocks but it did not work
$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();
Thanks
EDIT
I used below code to sort my problem and it worked perfectly :
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();
}
Answer
Solution:
Can you try the following:
$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();
Source