Filter Select elements with PHP

Asked

Viewed 913 times

1

I have to create a filter in select to perform a search between multiple select results

inserir a descrição da imagem aqui

<div class="grupo">
<!-- class="borda select2_single form-control comboauto" -->
    <select name="id_projeto" id="id_projeto" class="borda select2_single form-control ">
        <option value="">Selecione</option>
        <?php 
            foreach (ProjetoModel::retorna(" order by descricao") as $obj) { 
        ?>
            <option value="<?php echo $obj->getid() ?>" 

            <?php echo $obj->getid()==$vObject->getidProjeto()?'selected':'' ?>>
            <?php echo $obj->titulo; ?>
            </option>
        <?php } ?>
    </select>
</div>

inserir a descrição da imagem aqui

Return function

public static function retorna($extra='', $vWhere=null) {
        $arr=array();
        $sql = 'SELECT * FROM projeto where 1=1 ';
        $sql .= self::getWhere($vWhere);
        $sql .= $extra;
        $vResult = Conexao::getInstance()->query($sql);
            while($vResult && $obj = $vResult->fetchObject()) {
                $arr[] = new ClassProjeto($obj->id, $obj->titulo, $obj->descricao, $obj->id_net, $obj->id_trafo);
            }
            return $arr;
    }

JAVASCRIPT

<script type="text/javascript">
    var config = {
      '.chosen-select'           : {},
      '.chosen-select-deselect'  : {allow_single_deselect:true},
      '.chosen-select-no-single' : {disable_search_threshold:10},
      '.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},
      '.chosen-select-width'     : {width:"95%"}
    }
    for (var selector in config) {
      $(selector).chosen(config[selector]);
    }
  </script>

  • I couldn’t understand what it was. It would be like breaking folders?

  • Only perform a search inside the Html Select tag.

  • But the system is in php and

  • Ta but this search goes where?

  • I could understand there and that you are creating this select dynamically by the consultation made in the bank with the options you have in the bank. From what I reviewed your question, you want these options to come with filter and not all options, and this?

  • In this part here foreach (ProjetoModel::retorna(" order by descricao") you’re calling the function retorna() with 1 sort parameter order by descricao . Find this function and post here I pass you the answer.

  • Edited question...

  • The filter has to be inside the html select tag

Show 3 more comments

1 answer

1


<div class="grupo">
<!-- class="borda select2_single form-control comboauto" -->
<select name="id_projeto" id="id_projeto" class="borda select2_single form-control ">
    <option value="">Selecione</option>
    <?php 
        foreach (ProjetoModel::retorna(" order by descricao", " where coluna = termo ") as $obj) { 
    ?>
        <option value="<?php echo $obj->getid() ?>" 

        <?php echo $obj->getid()==$vObject->getidProjeto()?'selected':'' ?>>
        <?php echo $obj->titulo; ?>
        </option>
    <?php } ?>
</select>
</div>

Return function:

public static function retorna($extra='', $vWhere=null) {
    $arr=array();
    $sql = 'SELECT * FROM projeto ';
    $sql .= $vWhere;
    $sql .= $extra;
    $vResult = Conexao::getInstance()->query($sql);
        while($vResult && $obj = $vResult->fetchObject()) {
            $arr[] = new ClassProjeto($obj->id, $obj->titulo, $obj->descricao, $obj->id_net, $obj->id_trafo);
        }
        return $arr;
}

Watch out for the column and the term inside the foreach. If you want more filters, just use the and, ex: where coluna = termo and coluna2 = termo2

Browser other questions tagged

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