Php 如何使用$this->;db->;喜欢和$this->;db->;同时在codeigniter搜索表单中
大家好,我正试图通过在codeigniter中做一个求职项目来学习CI。我正试图使多输入域求职引擎在它。表单中有三个字段。我的表格在下面 我的控制器代码如下:Php 如何使用$this->;db->;喜欢和$this->;db->;同时在codeigniter搜索表单中,php,mysqli,codeigniter-3,Php,Mysqli,Codeigniter 3,大家好,我正试图通过在codeigniter中做一个求职项目来学习CI。我正试图使多输入域求职引擎在它。表单中有三个字段。我的表格在下面 我的控制器代码如下: class Search extends CI_Controller{ public function normal(){ $this->form_validation->set_rules('job_keywords', 'Job Keywords', 'required');
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子句。谁能告诉我哪里出了问题。多谢各位
尝试以这种方式构建查询:
$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\u search(…,$jobtitle,$jobdescription,$type\u id)
尝试以这种方式构建查询:
$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\u search(…,$jobtitle,$jobdescription,$type\u id)
还有一件事我正在比较数据库中的两列和表单中的第一个字段。位置字段是可选的。请尝试如下方式显示上次执行的查询echo$this->db->last_query()代码>我正在获取此信息-从作业
中选择*WHERE作业标题
如“%frontend%”转义“!”或者job\u description
像“%frontend%”“ESCAPE”!“而且type\u id
='pt'但是type\u id条件没有发挥任何作用,好像它根本不存在一样。你确定你的字符串中有一个id'pt'吗?还有一件事,我正在将数据库中的两列与表单中的第一个字段进行比较。位置字段是可选的。请尝试如下方式显示上次执行的查询echo$this->db->last_query()代码>我正在获取此信息-从作业
中选择*WHERE作业标题
如“%frontend%”转义“!”或者job\u description
像“%frontend%”“ESCAPE”!“而且type\u id
='pt'但是type\u id条件没有发挥任何作用,好像它根本不在那里一样。你确定你有一个id'pt'字符串吗?就在几分钟前,我做了同样的事情,但有点不同,它解决了问题。问题是我应该在查询中使用parantise将OR与AND分开。我做了这个$this->db->where(“(job_title
像“%”“$search_term.”“%”“ESCAPE”!”或job_description
像“%”“%”“$search_term.”“%%“ESCAPE”!”)和type_id
=“$type.”;$res=$this->db->get('jobs');谢谢你的回答。几分钟前,我做了同样的事情,但有点不同,解决了问题。问题是我应该在查询中使用parantise将OR与AND分开。我做了这个$this->db->where(“(job_title
像“%”“$search_term.”“%”“ESCAPE”!”或job_description
像“%”“%”“$search_term.”“%%“ESCAPE”!”)和type_id
=“$type.”;$res=$this->db->get('jobs');谢谢你的回答。这很有帮助