Filter with chekbox mysql php

Asked

Viewed 392 times

3

Could you help me as I would concatenate strings in this search, passing which table is right if it would be i or c?

select i.nome from imovel i join cidade c
where c.nome in ("Campo Grande","Paranaíba")
and i.tipo in ("Casa","Apartamento")
and i.dormitorio in ("4","5")

function parseMysqlQuery($array) {

    $output = '';
    foreach ($array as $key => $value) {
        $output .= !$output ? " WHERE $table.$key " : " AND $table.$key";
        $output .= ' IN ("' . implode('","', $value) . '")';
    }
    echo "Key: $key; Value: $value<br />\n";
    return $output;
}

$array = (array)filter_input_array(INPUT_POST,
        array( 'num' =>
                 array( 'filter' => FILTER_SANITIZE_STRING,
                        'flags' => FILTER_FORCE_ARRAY),
                        'grupo'  => array(
                            'filter' => FILTER_SANITIZE_STRING,
                            'flags'  => FILTER_FORCE_ARRAY,
                        ),
                        'dt_de_inducao' => array(
                            'filter' => FILTER_SANITIZE_STRING,
                            'flags'  => FILTER_FORCE_ARRAY,
                        ),
                )
        );

//SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'folha_tarefa' if ($_SERVER['REQUEST_METHOD'] == "POST") { $array = array_filter($array);

$in = parseMysqlQuery($array);

$query = "SELECT * FROM contratos " . $in;.

This is a filter with checkbox, only I have a search that I do a Join left and I’m not getting my php to understand which table to search, ex nometable.

  • 1

    Could you be more specific in your question?

  • Why opened two questions concerning the same subject, one of them being more incomplete?

  • Because I am learning to use the site and had not seen the edit button, why I opened 2

  • Do you really need to adopt this misspelled method? One remark, "concatenation" does not exist, the correct one is "concatenation". $table.$key would be nome_tabela.campo and $value the value of the table, then the correct one would be to pass an array structure that had each of these parameters. Being that $table, is not part of the function, but an external variable. $array = array(array('campo'=>'valor'),array('campo'=>'valor'), ...);

  • Thanks for the correction Ivan Ferrer, already corrected. well I need to do a filter and doing a search I found this method, (I am new in php programming), I started studying this code and I came across this problem of how to put the name of the table concatenated with the field, if you have any study link on the subject of filters so I can study thank you, I will try to use this form you wrote to me.

1 answer

1

Apparently $table.$key would be nome_tabela.campo and $value the table value, then the correct one would pass an array structure similar to this format:

$table = 'imovel';

$arrIn = array(
           array('c.nome'=> array('Campo Grande','Paranaíba')),
           array('i.tipo'=> array('Casa','Apartamento')),
           array('i.dormitorio'=> array('4','5'))
        );
$in = parseMysqlQuery($arrIn);

$query = "select i.nome from {$table} i join cidade c on(c.id_cidade=i.id_fk_cidade)
where c.nome " . $in;

Browser other questions tagged

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