mysql - php query array in array
I am using php phalcon5 framework. I have 2 columns on a product table. Product title and keyword. The product title contains 1 complete sentence and the keyword contains multiple comma separated words. I have done different queries but am getting unexpected results. How do I get the right results?
[Table]
table -> products
column -> product_title, keywords
[Query Results]
{-code-2}
[Controller]
{-code-3}
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Solution:
One thing to take in account is that you can use only one bind param, ex. data, and reemploy it in either keywords and product_title comparison:
$query = Products::find(["keywords LIKE :data: OR product_title LIKE :data:",
"bind" => ["data" => '%'. $data .'%'],
]);
In the other hand, you should to explode your query string to prevent it from affecting the words order, and repeat your keywords/product_title comparison with each of them:
$words = array_map('trim', preg_split('|\s+|', $this->request->getPost('q')));
$params = [];
foreach ($words as $key => $word) {
$params['conditions'] .= isset($params['conditions']) ? ' AND ' : '';
$params['conditions'] .= "(keywords LIKE :word".$key.": OR product_title LIKE :word".$key.":)";
$params['bind']['word'.$key] = '%'. $word .'%';
}
$query = Products::find($params);
Source