CodeIgniter Forums

Full Version: Multiple Keyword in search
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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'); 
Can anyone help me?
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); 
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
Look at my example again, you need multiple orLike.
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(); 
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();