1
I’m a beginner in PHP and I’m having a hard time creating the logic for the search system I’m implementing. I have a GET method form with three inputs and a select and need to mount the SQL variable only for GET other than null.
Follow the code I put together:
$filtro = array(
'NFe' => $_GET['nfe'],
'NFS' => $_GET['nfse'],
'status' => $_GET['status'],
'vencimento' => $_GET['vencimento']);
foreach($filtro as $chave => $valor){
if(!empty($valor)){
$sql = " and " .$chave. " = '" .$valor. "'";
}echo $sql; // Aqui ele traz quatro resultados, porem repete os que não estão vazio.
}
echo $sql; //Aqui ele traz apenas um resultado
All GET will be tested against the same column or different columns?
– Luis Alberto Batista
Luis, they’re different columns.
– Rodrigo Guedes
in foreach these to subscribe to the $sql variable and the previous value is not stored try
$sql .= "AND coluna = valor";
– 13dev
13dev, Thank you very much. It worked. I had managed to use
$sql[ ] = " and " .$chave. " = '" .$valor. "'";
and print like this$sql[0]. $sql[1]. $sql[2]. $sql[3];
But its shape reduces the code.– Rodrigo Guedes