Assuming the content of $_GET['category']
be it for example:
$_GET['category'] = "ação, comédia, romance";
You could do it like this:
$categorias = !empty($_GET['category']) ? $_GET['category'] : '';
$categorias = implode("', '", explode(', ', $categorias));
$sql = "SELECT *
FROM filme
WHERE
categoria IS NOT NULL AND
categoria IN ('{$categorias}')
ORDER BY nome LIMIT 25";
The excerpt $categorias = implode("', '", explode(', ', $categorias));
is doing the following:
The part explode(', ', $categorias)
explodes the contents of the variable that is "ação, comédia, romance"
in a array ["ação", "comédia", "romance"]
.
So the part implode("', '", explode(', ', $categorias))
"glue" that content that was "blown up" into a string using as a separator for each part to string ', '
then the result now is "ação', 'comédia', 'romance"
.
With this result we can use on condition IN
SQL to say that the field has to have a value similar to any of the elements ie categoria IN ('ação', 'comédia', 'romance')
which means the value of the field categoria
must be equal to ação
or comédia
or romance
.
Or you can even concatenate several LIKE
s, as I was doing:
$categorias = explode(', ', $categorias);
$sql = "SELECT *
FROM filme
WHERE
categoria IS NOT NULL AND (";
foreach ($categorias as $categoria) {
$sql .= " categoria LIKE '%{$categoria}%' OR ";
}
$sql = rtrim($sql, ' OR ') . ") ORDER BY nome LIMIT 25";
Ask the question the contents of the variable
$categoria
for us to get an idea of how you are receiving this data. To add more information just click on edit just below the question.– KaduAmaral
How to take more than one bank category from the same table
– rray