Tahmid-ni7
Tahmid-ni7

Reputation: 410

Facing problem at codeigniter multiple word search

I am creating a CodeIgniter search for my bookshop project. It is okay with a single word search but I am facing the problem when I search with multiple words. How I can fix this problem.

MY controller

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);
    }

}

MY Model

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();
}

Like if I write PHP in my search box it brings my all books which title have the word PHP. It is okay for me.

but when I write PHP BOOKS in my search box it shows me no book found.

I want it to show the result if any word is matched.

Upvotes: 3

Views: 297

Answers (1)

Rahul
Rahul

Reputation: 18557

You can replace this

$this->db->like('book_name', $query);

with condition as

$str = str_replace(" ","|", $query);
$this->db->where("book_name rlike '$str'");

You can get more details about rlike here.

Here is a better full text search help provided for your reference.

Upvotes: 1

Related Questions