Hello, I have some, given by me, an array of $myarray = array('Керосины', 'Абрикосы', 'Соли', 'Паспорта'); and also I have a database query, find these words from the array there:

 $stmt = $pdo->prepare('SELECT * FROM items WHERE itemsname LIKE :iname'); $stmt->execute(Array('iname' => '%'.$iname.'%')); 

where the $iname variable is my attempt to make the correct query $iname = implode(',',$myarray); In the end, of course, he simply searches for all 4 words together, separated by commas. Now the question is - How to make it look for each word in the array in turn? And put it in some field through a loop, if it coincides? Ie, in the end, I need to do so that by clicking on the yellow button I get the result in the green block below (I can write functionality, I just ask for help with the query and output). enter image description here

Update (Solution to my problem, thanks for the tip in the answers):

 $myarray = array('Керосины', 'Абрикосы', 'Соли', 'Паспорта'); $implod = implode('|',$weaparray); $total = count($myarray); foreach ($myarray as $key => $keyword) { echo '<a href="?type='.$keyword.'" class="iname">'.$keyword.'</a>'; } if (isset($_GET['type'])) { $stmt = $pdo->prepare('SELECT * FROM items WHERE itemsname LIKE :iname'); $stmt->execute(Array('iname' => $_GET['type'].'%')); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { } } 
  • you will most likely have to form a query string at the beginning with a certain amount of LIKE, and then cram an array of data ... that is, the row you should end up with is SELECT * FROM table WHERE column1 LIKE ? OR column1 LIKE ? OR column2 LIKE ? SELECT * FROM table WHERE column1 LIKE ? OR column1 LIKE ? OR column2 LIKE ? and then insert the data $query->execute(array("%$value1%","%$value2%",.....)); ... as one of the options - Alexey Shimansky
  • @ Alexey Shimansky, thanks for the option, but actually I gave only an example, but in reality I have an array of 30 elements, not 4. Ie, is it at least 30 OR,? and in the array itself to each element %%, so I could go the way of crutches and make just 4 buttons so that when pressed, for example, a GET request was sent and displayed all LIKE, but this time it is not an exit ( - 100ROZH
  • Well, what difference does it make to you 4 or 30, if the query string is formed in a loop, and not manually? And this is still not a query in the loop ........ can then try REGEXP ? SELECT * FROM items WHERE 1 = (SELECT itemsname REGEXP "val1|val2|val3"); - Alexey Shimansky
  • even simpler is SELECT * FROM items WHERE itemsname REGEXP "val1|val2|val3"; - Alexey Shimansky

1 answer 1

Try to make a request in your case not through LIKE , but through REGEXP .

In general, the request will be:

 SELECT * FROM `items` WHERE `itemsname` REGEXP "val1|val2|val3"; 

that is, it will be necessary to break the array not according to the sign,, but in a straight line |

that is, $iname = implode('|',$myarray);

Since the request

 WHERE `col` LIKE '%val1%' OR `col` LIKE '%val2%' ... 

in principle equivalent to such

 WHERE `col` REGEXP 'val1|val2|...' 

Or form the query string and the inserted data through the loop. Something like this:

 $keywords = array('Керосины', 'Абрикосы', 'Соли', 'Паспорта'); $totalKeywords = count($keywords); $query = "SELECT * FROM items WHERE itemsname LIKE ?"; for($i = 1 ; $i < $totalKeywords; ++$i){ $query .= " OR itemsname LIKE ? "; } $sql = $this->db->prepare($query); foreach ($keywords as $key => $keyword) { $sql->bindParam($key + 1, '%'.$keyword.'%'); } $sql->execute(); 
  • Ie, so that I can control which of the array words to use, it is better to use option 2? Because the first one uses OR and I have 3800 lines in a row, where there are values ​​from the array, and I still need to somehow create a cycle so that 4 <a> </a> elements are created, where it is written in each word from the array and when pressed for each, the actual results were revealed precisely of them, and not of all the crystals - 100ROZH
  • But, thank you so much, I tried, and then you reminded about foreach and how to use it, things got off the ground, in response to my question I would add what I wanted to receive - 100A FLASH