Multiple Keyword in search |
Hi, I am creating a search form but I have a problem, when I search for two keywords together "Key Keyword" returns me no results.
Controller: PHP Code: $keyword = esc($this->request->getVar('key')); With a keyword it works, it shows the results but unfortunately by inserting more than one it shows nothing. I pass the data into GET. My routes: PHP Code: $routes->get('search', 'Auth::search');
You put in "php search", are that what you mean by two keywords?
You need to split them up with explode(' ', $keywords); and search for them individually. Creating multiple orLike: e.g. PHP Code: $builder->like('first_name', $keyword1);
PHP Code: $keyword = explode(' ', $this->request->getVar('key')); Return error: Argument 2 passed to CodeIgniter\Database\BaseBuilder::like() must be of the type string, array given, called in
Do I have to duplicate the same input into two different variables?
PHP Code: $keyword = explode(' ', $this->request->getVar('key'));
No, that will just get you an error as well.
If you just want to use it directly with explode, you need to specify the index of the array. But you need to check how many keywords you got and add this X times: PHP Code: $builder->orLike('bio', $keyword[1]); This is for two words. But as you don't know how many keywords there are, you need to do it more dynamic and loop over it. PHP Code: $keyword = explode(' ', $this->request->getVar('key')); |
Welcome Guest, Not a member yet? Register Sign In |