下面是我的CI控制器代码,在查询中添加多个喜欢:
$this->load->library('pagination');
$currPage = ($this->uri->segment(4)) ? $this->uri->segment(4) : "" ;
$config['per_page'] = 7;
if($currPage<2)
$currPage = "";
//search
$prjSearch = ($this->uri->segment(3)) ? $this->uri->segment(3) : "" ;
if($prjSearch != "") {
$this->db->like('projectName', $prjSearch, 'after');
$this->db->like('projectName', $prjSearch, 'after');
$this->db->like('projectName', $prjSearch, 'after');
}
$config['base_url'] = $this->config->site_url()."project/index/".$prjSearch."/";
$this->db->select("project.*");
$query = $this->db->get('project',$config['per_page'],$currPage);
$data['data'] = $query->result();
echo $this->db->last_query();
$config['cur_page'] = $data['cur_page'] = $currPage;
$config['uri_segment'] = 4;
//total count
if($prjSearch != "")
$this->db->like('projectName', $prjSearch, 'after');
$query = $this->db->get('project');
$config['total_rows'] = $query->num_rows();
$this->pagination->initialize($config);
生成如下结果:
SELECT `project`.* FROM (`project`) WHERE `projectName` LIKE 'kesh%' AND `projectName` LIKE 'kesh%' AND `projectName` LIKE 'kesh%' LIMIT 7
而不是"AND"之间,就像我需要有"OR"。
我不想使用$this->db->query();
,因为分页将变得复杂或CI不支持
try with this
$this->db->or_like();
例如:
$this->db->like('title', 'match');
$this->db->or_like('body', $match);
在你的例子中,
$this->db->like('projectName', $prjSearch, 'after');
$this->db->or_like('projectName', $prjSearch, 'after');
$this->db->or_like('projectName', $prjSearch, 'after');
使用$this->db->or_like
获得。所以把like部分改成
$this->db->like('projectName', $prjSearch, 'after');
$this->db->like('projectName', $prjSearch, 'after');
$this->db->like('projectName', $prjSearch, 'after');
$this->db->or_like('projectName', $prjSearch, 'after');
$this->db->or_like('projectName', $prjSearch, 'after');
$this->db->or_like('projectName', $prjSearch, 'after');