Although what you are doing is subject to SQL Injection, you can use implode()
to transform an array to a string.
Assuming your $ _GET looks like this:
http://site.com.br/filmes?category=acao&category=romance&category=thriller
You could do something like this:
$categoria = $_GET['category'];
$likes = implode("%' OR categoria LIKE '%",$categoria);
/* esse implode faz um array assim:
[0] => 'acao',
[1] => 'romantico',
[2] => 'thriller'
ficar assim:
"acao%' OR categoria LIKE '%romantico%' OR categoria LIKE '%thriller"
*/
$sql = "SELECT * FROM filme WHERE categoria IS NOT NULL AND ( categoria LIKE '%$likes%' ) ORDER BY nome LIMIT 25";
The explode()
takes a string and becomes an array, for example:
$str = "este,e,um,string";
$array = explode(",",$str);
//devovle:
/*
[0] => "este",
[1] => "e",
[2] => "um",
[3] => "string"
*/
So to use explode()
, $ _GET would have to look like this:
http://site.com.br/filmes?category=acao,romance,thriller
And then the only thing that changes is:
$categoria = explode(',', $_GET['category']);
Elaboration
It has been clarified that your problem is that acao
also returns animacao
due to the same acao
.
In this case, you would have to change your SQL. You have a few options:
Use IN()
to match right. Thus, it returns movies where category is EQUAL "action", but not "animation" or "action-terror".
SELECT * FROM filme WHERE categoria IN ( 'acao' )
Use LIKE
, but less %
. So, it returns movies where category has "action" in front and not as end of word.
SELECT * FROM filme WHERE categoria LIKE 'acao%'
Here is a SQLFiddle , showing an example of both.