如何在codeigniter搜索表单

时间:2016-04-28 15:16:05

标签: php mysqli codeigniter-3

大家好我想通过在codeigniter中创建一个Job Search项目来学习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

1 个答案:

答案 0 :(得分:0)

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

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