Array output from multiple select option
Array
(
[q] => Array
(
[0] => monkey
[1] => pig
[2] => dog
[3] => banana
[4] => apple
[5] => mango
[6] => lavender
[7] => magnolia
[8] => marigold
)
)
The code
$queryArray = array_map("secure", $_GET['q']);
$inArray = array();
foreach ($queryArray as $key) {
$inArray[] = 'animals="' . $key . '"';
}
$sql = 'SELECT * FROM kids WHERE ' . implode(' AND ', $inArray) . '';
echo $sql;
Question :
How do I group the current array to be
animals = monkey,pig,dog
fruits = banana,apple,mango
flowers = lavender,magnolia, marigold
Maybe filter the value manually or if in_array monkey,pig,dog is animals.
and make a correct MySQL statement to get something like this
SELECT *
FROM kids
WHERE
(animals="monkey" OR animals="pig" OR animals="dog")
AND
(fruits="banana" OR fruits="apple" OR fruits="mango")
AND
(flowers="lavender" OR flowers="magnolia" OR flowers="marigold")
Let me know.
You can use PHP associative array to store the query like this: