Welcome Guest, Not a member yet? Register   Sign In
Multiple Keyword in search
#1

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'); 
Reply
#2

Can anyone help me?
Reply
#3

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); 
Reply
#4

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
Reply
#5

Look at my example again, you need multiple orLike.
Reply
#6

(This post was last modified: 04-18-2020, 09:47 AM by Marcolino92.)

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(); 
Reply
#7

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




Theme © iAndrew 2016 - Forum software by © MyBB