PHP+Mysql Search System

Asked

Viewed 236 times

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, they’re different columns.

  • in foreach these to subscribe to the $sql variable and the previous value is not stored try $sql .= "AND coluna = valor";

  • 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.

2 answers

0


Good afternoon Rodrigo. You can use two different strategies. I understood that they are filters and you can check in the code if the field is null and add to your search condition (WHERE) or you can mount the condition with all fields and test the field condition. Something like:

Where (@condicao1 is null or campo1 = @condicao1)

Thus, if @condicao1 is null it passes and does not check the comparison with the field and thus only the non-null variables will be compared with the values of the fields in the condition.

  • I followed your logic and created the above code using foreach. Thank you very much.

  • If the answer was useful, do not forget to mark by clicking the up arrow!

0

Rodrigo, test this shape and tell me if it fits. In my imagination it seems to work SURROUNDING ONLY THE $_GET DEFINED.

<?php
$get = [];

if(isset($_GET['nfe']) && !empty($_GET['nfe'])) {
    $nfe = $_GET['nfe'];
    $get[] = "nfe = '$nfe'";
}
if(isset($_GET['nfse']) && !empty($_GET['nfse'])) {
    $nfse = $_GET['nfse'];
    $get[] = "nfse = '$nfse'";
}
if(isset($_GET['status']) && !empty($_GET['status'])) {
    $status = $_GET['status'];
    $get[] = "status = '$status'";
}
if(isset($_GET['vencimento']) && !empty($_GET['vencimento'])) {
    $vencimento = $_GET['vencimento'];
    $get[] = "vencimento = '$vencimento'";
}

$whereCase = join(" AND ", $get);

print $whereCase;
?>

Based on your logic it would look something like this:

<?php
$filtro = array(
    'NFe' => isset($_GET['nfe']) ? $_GET['nfe'] : "",
    'NFS' => isset($_GET['nfse']) ? $_GET['nfse'] : "",
    'status' => isset($_GET['status']) ? $_GET['status'] : "",
    'vencimento' => isset($_GET['vencimento']) ? $_GET['vencimento'] : ""
);

$sql = [];

foreach($filtro as $chave => $valor) {
    if($valor != ""){
        $sql[] = "$chave = '$valor'";
    }
}

$whreCase = join(" AND ", $sql);

echo $whreCase; //Aqui ele traz apenas um resultado
?>
  • Sorry it took so long. I’ll run the test and tell you if it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.