Multiple Keyword in search - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Multiple Keyword in search (/showthread.php?tid=76135) |
Multiple Keyword in search - Marcolino92 - 04-16-2020 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'); RE: Multiple Keyword in search - Marcolino92 - 04-18-2020 Can anyone help me? RE: Multiple Keyword in search - jreklund - 04-18-2020 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); RE: Multiple Keyword in search - Marcolino92 - 04-18-2020 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 RE: Multiple Keyword in search - jreklund - 04-18-2020 Look at my example again, you need multiple orLike. RE: Multiple Keyword in search - Marcolino92 - 04-18-2020 Do I have to duplicate the same input into two different variables? PHP Code: $keyword = explode(' ', $this->request->getVar('key')); RE: Multiple Keyword in search - jreklund - 04-18-2020 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')); |