$option_meno = ["Lukas", "Ivka"];
$sql = "SELECT *
FROM uctovnictvo
WHERE meno IN ('$option_meno[1]', '$option_meno[0]')
AND datum BETWEEN '$date_start' AND '$date_end'
ORDER BY $order ";
For sure there has to be a better way how to select user based on name (meno
). There can be more or fewer names in the $option_meno
array.
I would like to make especially this more simple than listing out each index in the option array ('$option_meno[1]','$option_meno[0]')
.
You can use implode()
:
$sql = " SELECT *
FROM uctovnictvo
WHERE meno IN ('" . implode('\', \'', $option_meno) . "')
AND datum BETWEEN '$date_start' AND '$date_end'
ORDER BY $order ";
You could use some array functions to auto generate the correct IN
statement
$option_meno = ["Lukas","Ivka"];
$in = implode(',', array_map(function($item) use ($pdo) {
return '"' . $pdo->quote($item) . '"';
}, $option_meno);
$sql = "SELECT * FROM uctovnictvo WHERE meno IN ($in)...";
instead of PDO::quote
you could use also mysqli_real_escape_string
, etc. (depends on your connection).