如何在代码点火器搜索表单中同时使用 $this->db->like 和 $this->db->where


How to use $this->db->like and $this->db->where at the same time in codeigniter search form

大家好,我正在尝试通过在代码点火器中制作求职项目来学习 CI。我正在尝试在其中制作多输入字段工作搜索引擎。我在表单中有三个字段。我的表格如下表单屏幕截图

我的控制器代码是这样的:

  class Search extends CI_Controller{
    public function normal(){
        $this->form_validation->set_rules('job_keywords', 'Job Keywords', 'required');
        if($this->form_validation->run() == FALSE){
             $viewdata['main_view'] = 'home';
             $this->load->view('layout/main', $viewdata);
        }
        else{
          $search_term = $this->input->post('job_keywords');
          $location = $this->input->post('job_location');
          $type = $this->input->post('job_type');
          $searchdata['search_results'] = $this->search_model->default_search($search_term, $location, $type);
                $searchdata['main_view'] = 'search_page';
          $this->load->view('layout/main', $searchdata);
        }
    }

}

我的模型是:

    public function default_search($search_term, $location="", $type){
    $searchterm = $search_term;
    $this->db->like('job_title', $searchterm);
    $this->db->or_like('job_description', $search_term);
    $this->db->where('type_id', $type); 
    if($location!= ""){
       $this->db->like('location_id', $location);   
    }

    $res = $this->db->get('jobs');
    if($res->num_rows() >= 1){
        return $res->result();
    }
    else{
        return false;
    }
}
当我只使用 $this->$db->like() 和 $this->$db->or_like() 时,

我得到了搜索结果,但是当我使用 $this->$db->where() 时,查询不遵循 WHERE 子句。当我删除 $this->$db->or_like() 时,它可以正常工作,但不能使用两个类似的子句。有人可以告诉我哪里出错了。谢谢。

包含 2 行数据的作业表结构

尝试以这种方式构建查询:

$query = $this->db->query(" SELECT * FROM jobs WHERE (job_title LIKE '%".$jobtitle."%' OR job_description LIKE '%".$jobdescription."%') AND type_id = '".$type_id."' ");
if ($query->num_rows() > 0){
  foreach ($query->result() as $row){
     //do some code
    }
  }

尝试在函数中传递 3 个参数: public function default_search(..., $jobtitle, $jobdescription,$type_id)