I’m working on a search query for my database using CodeIgniter.
I already have a working query for searching with a single word, but it's not good for individually searching multiple words.
My single-word 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, I got closer to what I need by looping over the exploded string:
$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 a good query?
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.