CodeIgniter Forums
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'));

$builder $this->db->table('mytable');
$builder->like('first_name'$keyword);
$builder->orLike('last_name'$keyword);
$builder->orLike('bio'$keyword);
$builder->groupBy("id");

$query $builder->get();
$q $query->getResultArray();
        
$data
['search'] = $q

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);
$builder->orLike('last_name'$keyword1);
$builder->orLike('bio'$keyword1);
$builder->orlike('first_name'$keyword2);
$builder->orLike('last_name'$keyword2);
$builder->orLike('bio'$keyword2); 



RE: Multiple Keyword in search - Marcolino92 - 04-18-2020

PHP Code:
$keyword explode(' '$this->request->getVar('key'));

$builder $this->db->table('test');
$builder->like('bio'$keyword);
$builder->orLike('first_name'$keyword);
$builder->orLike('last_name'$keyword);
$query $builder->get();

$q $query->getResultArray(); 


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'));
$keyword2 explode(' '$this->request->getVar('key'));

$builder $this->db->table('test');
$builder->like('bio'$keyword);
$builder->orLike('first_name'$keyword);
$builder->orLike('last_name'$keyword);
$builder->orLike('bio'$keyword2);
$builder->orLike('first_name'$keyword2);
$builder->orLike('last_name'$keyword2);
$query $builder->get();

$q $query->getResultArray(); 



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]);
$builder->orLike('first_name'$keyword[1]);
$builder->orLike('last_name'$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'));

$builder $this->db->table('test');
$builder->like('bio'$keyword[0]);
$builder->orLike('first_name'$keyword[0]);
$builder->orLike('last_name'$keyword[0]);
$builder->orLike('bio'$keyword[1]);
$builder->orLike('first_name'$keyword[1]);
$builder->orLike('last_name'$keyword[1]);
$query $builder->get();

$q $query->getResultArray();