如何在 codeigniter 搜索表单中同时使用 $this->db->like 和 $this->db->where

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

大家好,我正在尝试通过在 codeigniter 中创建求职项目来学习 CI。我正在尝试在其中制作多个输入字段的职位搜索引擎。我在表单中有三个字段。我的表格如下 Form screenshot

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

  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() 时,它工作正常但没有两个 like 子句。有人可以告诉我哪里出错了。谢谢。

Job table structure with 2 rows of data

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

$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)