Flexible Search filter

0

I have a page with a search filter with the options "service", "state" and "city" and a submit button that redirects to the page where a table with the already filtered records will appear.

But currently the filter only works if the user only fills the service, or service and state or service and state and city, which prevents it from searching all the services that exist in that state or that state and city.

I would like to know how I can make the filter continue to work in case the user only fills the service, or service and state, or state and city service, and also works in case the user only fills the service or state and city.

This is the filter code:

<!-- - - - - - Esse é o teste do filtro - - - - - - -->
<?
    error_reporting(E_ERROR | E_PARSE);
    $lnk = mysqli_connect('localhost','root','') or die(mysqli_error()) or die ('Nao foi possível conectar ao MySql: ' . mysqli_error($lnk));
    mysqli_select_db($lnk,'db_qualquer') or die ('Nao foi possível ao banco de dados selecionado no MySql: ' . mysqli_error($lnk));


    $sql = 'SELECT * FROM teste ORDER BY servico, estado, cidade ASC';
    $servico = isset($_POST['servico']) ? $_POST['servico'] : null;
    $estado = isset($_POST['estado']) ? $_POST['estado'] : null;
    $cidade = isset($_POST['cidade']) ? $_POST['cidade'] : null;
    $arrParams = array();


    $sqli = "SELECT * FROM teste WHERE ";
    if(!is_null($servico) && !empty($servico)){
        $arrParams[] = array (
            'filter' => 'servico',
            'value' => $servico
        );
    }
    if (!is_null($estado) && !empty($estado) && $cidade == 'none' && $estado != 'none') {
        $arrParams[] = array (
            'filter' => 'estado',
            'value' => $estado
        );
    }

    if ( !is_null($estado) && !empty($estado) && !is_null($cidade) && !empty($cidade) && $cidade != 'none') {
        $arrParams[] = array (
            'filter' => 'estado',
            'value' => $estado
        );
        $arrParams[] = array (
            'filter' => 'cidade',
            'value' => $cidade
        );
    }
    $cont = 1;
    $total = count($arrParams);

    foreach($arrParams as $param){

        $sqli .= $param['filter'] . " LIKE '%".$param['value']."%'";
        if ($total > 1 && $cont != $total) {
            $sqli.= "AND ";
        }
        $cont ++;
    } 

    $qry = mysqli_query($link, $sqli) or die(mysqli_error($lnk));
    $count = mysqli_num_rows($qry);
    $num_fields = mysqli_num_fields($qry);//Obtém o número de campos do resultado
    //$fields[] = array();
    if($num_fields > 0) {
        for($i = 0;$i<$num_fields; $i++){//Pega o nome dos campos
            $fields[] = mysqli_fetch_field_direct($qry,$i)->name;
        }
    }

?>

If anyone can give me any tips it would be cool.

    
asked by anonymous 06.12.2017 / 11:59

0 answers