Question

I have to search for multiple values in a field using mysql in codeigniter. Here follows my code.

In Controller

public function vpsearch()
{
  $data['info'] = $this->psearch_m->emp_search_form();

  $this->load->view("employer/result",$data);       

}

IN Model

public function emp_search_form()
{
  $skill = $this->security->xss_clean($this->input->post('ps_skills'));
  $jrole = $this->input->post('ps_jobrole'));


  if ( $jrole !== NULL) 
  {
    return $this->db->get('js_edu_details');
    $this->db->like('js_skills','$skill');
  }
}

In view i.e, (../employer/result)

foreach($info->result() as $row)
{
  echo $row->js_id."<br/><br/>" ;
}

However I am getting all the records in 'js_edu_details' table instead of fields having searched 'skills'.

Where I am going wrong? Any help wud b appreciated, thanx in advance.

Was it helpful?

Solution

Try:

public function emp_search_form()
{
    $skill = $this->security->xss_clean($this->input->post('ps_skills'));
    //$skill = $this->input->post('ps_skills', true);  other short way of getting the above result with `xss clean`
    if ( $jrole !== NULL) 
    {
        $this->db->like('js_skills',$skill); #remove the single quote around the `$skill`
        $res = $this->db->get('js_edu_details');
        echo $this->db->last_query(); #try to print the query generated
        return $res;
    }
}

Return statement should be after the like statement

OTHER TIPS

You should arrange the code properly like this

public function emp_search_form()
{
    $ps_skills  =   $this->input->post('ps_skills')
    $skill      = $this->security->xss_clean($ps_skills);

    if ( $jrole !== NULL) 
    {
        $this->db->like('js_skills','$skill');
        return $this->db->get('js_edu_details');
    }
}

Also you should note the condition will never meet. It will always give error undefined variable $jrole

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top