【发布时间】:2019-04-19 13:06:09
【问题描述】:
我正在为我的书店项目创建 CodeIgniter 搜索。单个单词搜索没问题,但是当我搜索多个单词时我遇到了问题。我该如何解决这个问题。
我的控制器
public function search()
{
/*=== LOAD DYNAMIC CATAGORY ===*/
$this->load->model('admin_model');
$view['category'] = $this->admin_model->get_category();
/*==============================*/
$this->form_validation->set_rules('search_book', "Search",'required');
if($this->form_validation->run() == FALSE)
{
#...Redirected same page after action
redirect($_SERVER['HTTP_REFERER']);
}
else
{
$query = $this->input->post('search_book');
$this->load->model('user_model');
$view['books'] = $this->user_model->search($query);
$view['user_view'] = "users/search_books";
$this->load->view('layouts/user_layout', $view);
}
}
我的模特
public function search($query)
{
$this->db->order_by('id', 'DESC');
$this->db->from('books');
$this->db->like('book_name', $query);
$this->db->where('status', 1);
$q = $this->db->get();
return $q->result();
}
就像我在搜索框中写 PHP 一样,它会带来我所有标题中包含 PHP 的书籍。对我来说没问题。
但是当我在搜索框中写 PHP BOOKS 时,它显示我没有找到任何书。
如果有任何单词匹配,我希望它显示结果。
【问题讨论】:
-
您正在搜索哪个字符串
PHP BOOKS,我们可以获取示例字符串吗? -
您在搜索中的
like子句查询可能需要像这样$this->db-like('book_name', '%'.$query.'%' -
@RahulMeshram 针对 $query 字符串。
-
我的意思是,
PHP PHP PHP或BOOKS BOOKS BOOKS或PHP BOOKS PHP的示例字符串,你明白了吗? -
@RahulMeshram 好的,我明白了。没有像 php 书籍这样的书名。我想要的是,如果我写 php 书,它会给我带来这些名为 php 的书。示例字符串 PHP 和 Mysql。这是我数据库中的书名。
标签: php codeigniter full-text-search