Flexible search filter

Asked

Viewed 75 times

0

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

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

I wanted to know how I do to make the filter continue to work in case the user fills only the service, or service and state, or service state and city and also starts to work in case the user fills only 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 hints it would be nice.

  • place an echo($sqli) after the mysqli_query(...) and put what is being printed on the screen so that we can see the query

  • Hi! I tried searching filling only the state filter and gave: SELECT * FROM WHERE SERVICE LIKE '%Select...%'AND status LIKE '%Rio de Janeiro%'

No answers

Browser other questions tagged

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