CodeIgniter搜索查询

问题描述:

I’m working on some script and I would like to create search. My project is based on CodeIgniter and my desire is to have all code content compatibile with it. I've already written question like that, but I didn't get enough help and nobody wants to help me, so I opened this question.

I have already working on the query for searching but it’s not good because it doesn’t support more words than one. So if I enter word “test” in my search form (assume that the test word is in database in one of fields) there will be few results, but if I enter words “test test test” (again assume that the test words are in database in one of fields) there will be no any result.

My previous query:

$this->db->select('title, content, date, hyperlink')->or_like(array('title' => $query, 'content' => $query))->order_by('id_article', 'desc')->get('news');

And after some tweaks:

$this->db->select('title, content, date, hyperlink');
foreach ($parts as $q)
{
    $this->db->or_like(array('title' => $q, 'content' => $q));
}
$this->order_by('id_article', 'desc')
$show_results = $this->db->get('news');

What's the best way to create good query?

我正在编写一些脚本,我想创建搜索。 我的项目基于CodeIgniter,我的愿望是让所有代码内容与之兼容。 我已经写了这样的问题,但我没有得到足够的帮助,也没有人想帮助我,所以我打开了这个问题。 p>

我已经在处理搜索查询 但它并不好,因为它不支持更多的单词。 因此,如果我在我的搜索表单中输入单词“test”(假设测试单词位于其中一个字段的数据库中),则结果很少,但如果我输入单词“test test test”(再次假设测试单词是 在其中一个字段的数据库中)将没有任何结果。 p>

我之前的查询: p>

  $ this-> db-  >选择('标题,内容,日期,超链接') - > or_like(数组('title'=> $ query,'content'=> $ query)) - > order_by('id_article','  desc') - > get('news'); 
  code>  pre> 
 
 

经过一些调整后: p>

  $  this-> db-> select('title,content,date,hyperlink'); 
foreach($ parts as $ q)
 {
 $ this-> db-> or_like(array('title)  '=> $ q,'content'=> $ q)); 
} 
 $ this-> order_by('id_article','desc')
 $ show_results = $ this-> db-  > get('news'); 
  code>  pre> 
 
 

创建良好查询的最佳方法是什么? p> div>

Try out this code. This will help you more in your search query. It will search for any number of words wherever that word(s) exist.

$this->db->select('title, content, date, hyperlink');
$this->db->where("title LIKE '%$query%' OR content LIKE '%$query%'");

Hope this helps you.