SQL search for more than one type of property, city and neighborhood does not work properly

Asked

Viewed 292 times

0

I got a template ready with some problems in plugins and queries.

Of the 6 select fields to do a real estate search, 3 of them can select more than once, property type, city and neighborhood.

The list of these 3 fields is generated through the query and displayed with the checkbox before the names, in the case of the Immovable Type would be like this:

Tipo de Imóvel,

Before, only the Property Type could choose more than one item, then the customer asked that the City and Neighborhood fields could also choose more than one item. Well, the plugin I use is jquery.multiselect to do this. I manage to implement this despite that it was facing Property Type because of the description "Property Type" in the code that then replace with "Select an item".

So far so good, only when you do the research with more than one item (Property Type, City and/ or Neighborhood) only consider one last item marked in each one.

Example: when I mark first New Lima and then I mark Belo Horizonte, the search will return only Belo Horizonte.

Here’s the code:

<!-- BUSCAS -->
        <section class="boxBuscas">
            <div class="centerSite">

                <ul class="nav navBuscas">
                    <li id="bRapida"><a href="javascript:;">Busca <br /> Rápida</a></li>
                    <li id="bCodigo"><a href="javascript:;">Busca <br /> Código</a></li>
                    <li id="bAvancada"><a href="javascript:;">Busca <br /> Avançada</a></li>
                </ul>

                <div class="tpBuscas boxBuscas">
                    <h3 class="titles" id="encontre">Encontre seu Imóvel.</h3>

                    <?php
                    if ($modulo == "") {
                        $sql = "SELECT DISTINCT value, print FROM tipos WHERE codimobiliaria = '$codimobiliaria' ORDER BY print";
                    } else {
                        $sql = "SELECT * FROM tipos_$modulo WHERE (codimobiliaria='$codimobiliaria') ORDER BY print";
                    }

                    $first_query = mysqli_query($conexao,$sql);
                    $second_query = mysqli_query($conexao,$sql);
                    $linhas = mysqli_num_rows($first_query);
                    ?>                


                    <div class="forms bRapida">
                        <form method="GET" id="frmBusca" name="frmBusca" action="<?php echo $url_arquivo; ?>lista.php">
                            <dl class="pretensao">
                                <dt><label id="lblPret" name="lblPret" for="ddlPretensao">Quero:</label></dt>
                                <dd>
                                    <select id="ddlPretensao" name="modulo">
                                        <option value="1">Comprar</option>
                                        <option value="2" selected >Alugar</option>
                                    </select>
                                </dd>
                            </dl>

                            <dl class="tipo">
                                <dt><label id="lbltipo" name="lbltipo" for="ddltipo">Tipo do Imóvel:</label></dt>
                                <dd>
                                    <select id="ddltipo" name="ddltipo" multiple="multiple">
                                        <option value="0" selected="selected">Todos</option>
                                        <?php
                                        //Loop no primeiro campo de tipos de imovel.
                                        for ($i = 0; $i < $linhas; $i++) {
                                            $type_01 = mysqli_fetch_array($first_query);
                                            echo "<option value=\"$type_01[value]\">$type_01[value]</option>";
                                        }
                                        ?>
                                    </select>
                                </dd>
                            </dl>
                            <script type="text/javascript">$(function () {
                                    $('select[id=ddltipo]').multiselect();
                                });</script>

                            <?php

                            $sql_cidade = "SELECT cidade FROM vendas WHERE codimobiliaria = $codimobiliaria GROUP BY cidade";
                            $res = mysqli_query($conexao,$sql_cidade);
                            ?>

                            <dl class="tipo" >
                                <dt><label id="lblCidade" name="lblCidade" for="ddlCidade">Cidade:</label></dt>
                                <dd>
                                    <select id="cidade" name="cidade" multiple="multiple">
                                        <option value="0" selected="selected">Cidade</option>

                                        <?php
                                        while ($row = mysqli_fetch_array($res)) {
                                            echo "<option value='" . $row['cidade'] . "'> " . utf8_encode($row['cidade']) . "</option>";
                                        }
                                        ?>


                                    </select>
                                </dd>
                            </dl>
                            <script type="text/javascript">$(function () {
                                    $('select[id=cidade]').multiselect();
                                });</script>


                            <?php
                            if ($modulo == "") {
                                $sql = "SELECT DISTINCT value, print FROM bairros WHERE (codimobiliaria='$codimobiliaria') ORDER BY print";
                            } else {
                                $sql = "SELECT * FROM bairros_$modulo WHERE (codimobiliaria='$codimobiliaria') ORDER BY print";
                            }

                            $first_query = mysqli_query($conexao,$sql);
                            $second_query = mysqli_query($conexao,$sql);
                            $linhas = mysqli_num_rows($first_query);
                            ?>

                            <dl class="tipo">
                                <dt><label id="lblBairro">Bairro:</label></dt>
                                <dd>
                                    <select id="ddlBairro" name="ddlBairro" multiple="multiple">
                                        <option value="0" selected="selected">Bairro</option>

                                        <?php
                                        //Faz o loop com os registros de bairro dentro do Box de select.
                                        for ($i = 0; $i < $linhas; $i++) {
                                            $type_01 = mysqli_fetch_array($first_query);
                                            echo utf8_encode("<option value=\"$type_01[value]\">$type_01[print]</option>");
                                        }
                                        ?>
                                    </select>

                                </dd>
                            </dl>
                            <script type="text/javascript">$(function () {
                                    $('select[id=ddlBairro]').multiselect();
                                });</script>


                            <dl class="vagas">
                                <dt><label id="lblQts" name="lblQts" for="ddlQts">Quartos:</label></dt>
                                <dd>
                                    <select id="ddlQts" name="ddlQts">
                                        <option value="">Quartos</option>
                                        <option value="1">A partir de 1</option>
                                        <option value="2">A partir de 2</option>
                                        <option value="3">A partir de 3</option>
                                    </select>
                                </dd>
                            </dl>

                            <div class='size1'><dl class="valorMax">
                                    <dt><label id="lblValorMax" name="lblValorMax" for="txtValorMax">Valor máximo:</label></dt>
                                    <dd>
                                        <select name="ddlValorMax" id="ddlValorMax" />
                                        </select>
                                    </dd>
                                </dl>
                            </div>

                            <input type="submit" id="btnEnviar" class='' value="Buscar" />
                            <input type="hidden" id="remetente"  value="1" /></form>

                        <div class="clearboth"></div>
                    </div><!-- .bRapida -->

                    <div class="forms bCodigo">
                        <form method="GET" id="frmBusca" name="frmBusca" action="<?php echo $url_arquivo; ?>listagem.php">


                            <dl class="pretensao">
                                <dt><label id="modulo" name="modulo" for="modulo">Quero:</label></dt>
                                <dd>
                                    <select id="modulo" name="modulo">
                                        <option value="vendas">Comprar</option>
                                        <option value="aluguel">Alugar</option>
                                    </select>
                                </dd>
                            </dl>                    

                            <p>( Digite o código dos imóveis desejados. Exemplo: CA0001, CA0002 )</p>
                            <input id="ref" type="text" name="ref" onblur="if (this.value == '') {
                                        this.value = 'Código';
                                    }" onfocus="if (this.value == 'Código') {
                                                this.value = '';
                                            }" value='Código' />
                            <input type="submit" id="btnEnviar" class='' value="Buscar" />

                            <input type="hidden" id="remetente"  value="1" /></form>
                    </div><!-- .bCodigo -->

                    <div class="forms bAvancada">
                    </div><!-- .bAvancada -->
                </div><!-- .tpBuscas -->

                <div class="clearboth"></div>
            </div><!-- .centerSite -->
        </section><!-- .boxBuscas -->

Can someone help me?

EDIT: A detail that I realized only later. It’s the list.php file where the action sends. It receives the data of the search and shows the selected search.

<div id="pageListagem" class="pageInternal listagemPadrao">

    <div id="listImovel">

<?php

//$codigo_imovel = $_GET['ref'];

$modulo = $_GET['pretensao'];
$pes_tipo_imovel = $_GET['tipo'];
$pes_cidade = $_GET['cidade2'];
$pes_bairro = $_GET['ddlBairro'];
$pes_quartos = $_GET['ddlQts'];
$pes_valor = $_GET['ddlValorMax']; 


if($modulo == '1'){ 
    $tabela = 'vendas';
    $oque = 'Valor de venda:';
    $pasta = 'img_vendas/';
}elseif($modulo == '2'){    
    $tabela = 'aluguel';
    $oque = 'Valor de Locação:';    
    $pasta = 'img_aluguel/';    
} 

?>        

        <!-- listagem de imoveis -->
        <div class="listagemImovel">

        <?php 

        //$sql2 = "SELECT * FROM $tabela WHERE codimobiliaria = '$codimobiliaria' && imovel != 'Industrial' && imovel != 'Rural'";

        if($pes_cidade != '0'){
            $sql2 = "SELECT * FROM $tabela WHERE codimobiliaria = '$codimobiliaria' && cidade = '$pes_cidade' && imovel != 'Industrial' && imovel != 'Rural'";  
        }if($pes_bairro != '0'){
            $sql2 = "SELECT * FROM $tabela WHERE codimobiliaria = '$codimobiliaria' && bairro = '$pes_bairro' && imovel != 'Industrial' && imovel != 'Rural'";  
        }if($pes_tipo_imovel != '0'){
            $sql2 = "SELECT * FROM $tabela WHERE codimobiliaria = '$codimobiliaria' && imovel = '$pes_tipo_imovel' && imovel != 'Industrial' && imovel != 'Rural'"; 
        }if($pes_quartos != ''){
            $sql2 = "SELECT * FROM $tabela WHERE codimobiliaria = '$codimobiliaria' && nquartos >= '$pes_quartos' && imovel != 'Industrial' && imovel != 'Rural'";                  
        }if($pes_valor != '99999999'){
            $sql2 = "SELECT * FROM $tabela WHERE codimobiliaria = '$codimobiliaria' && preco <= '$pes_valor' && imovel != 'Industrial' && imovel != 'Rural'";                   
        }if($pes_tipo_imovel != '0' && $pes_cidade != '0'){
            $sql2 = "SELECT * FROM $tabela WHERE codimobiliaria = '$codimobiliaria' && imovel = '$pes_tipo_imovel' && cidade = '$pes_cidade' && imovel != 'Industrial' && imovel != 'Rural'";   
        }if($pes_tipo_imovel != '0' && $pes_cidade != '0' && $pes_bairro != '0'){
            $sql2 = "SELECT * FROM $tabela WHERE codimobiliaria = '$codimobiliaria' && imovel = '$pes_tipo_imovel' && cidade = '$pes_cidade' && bairro = '$pes_bairro' && imovel != 'Industrial' && imovel != 'Rural'"; 
        }if($pes_tipo_imovel != '0' && $pes_cidade != '0' && $pes_bairro != '0' && $pes_quartos != '0'){
            $sql2 = "SELECT * FROM $tabela WHERE codimobiliaria = '$codimobiliaria' && imovel = '$pes_tipo_imovel' && cidade = '$pes_cidade' && bairro = '$pes_bairro' && nquartos >= '$pes_quartos' && imovel != 'Industrial' && imovel != 'Rural'";   
        }if($pes_tipo_imovel != '0' && $pes_cidade != '0' && $pes_bairro != '0' && $pes_quartos != '0' && $pes_valor != '99999999'){
            $sql2 = "SELECT * FROM $tabela WHERE codimobiliaria = '$codimobiliaria' && imovel = '$pes_tipo_imovel' && cidade = '$pes_cidade' && bairro = '$pes_bairro' && nquartos >= '$pes_quartos' && preco <= '$pes_valor' && imovel != 'Industrial' && imovel != 'Rural'";  
        }if($pes_tipo_imovel == '0' && $pes_cidade == '0' && $pes_bairro == '0' && $pes_quartos == '' && $pes_valor == '99999999'){
            $sql2 = "SELECT * FROM $tabela WHERE codimobiliaria = '$codimobiliaria' && imovel != 'Industrial' && imovel != 'Rural'";            
        }

        ?>

            <?php
                $query2 = mysqli_query($conexao,$sql2);
            if(mysqli_num_rows($query2)){
               $reg2 = mysqli_fetch_array($query2);

            }

             ?>         

        <?php $linhas = mysqli_num_rows($query2); ?>

            <header class="headerListagem">
                <h2 class="titleBox">Resultado da Busca </h2>
                <p class="qntImoveis"><strong><?=$linhas?></strong> imóveis encontrado(s)</p>                
                <div class="opcoes">
                    <div id="dv_order" name="dv_order" class="filtros"><p>Ordenar por: </p>
                        <select id='ddlorder' name='ddlorder'>
                            <option value='menor' selected='selected' >menor valor</option>
                            <option value='maior' >maior valor</option>
                        </select>

                        <input type="hidden" id="order" name="order" value="3"></div>

                        <div class="modoVisualizacao">
                            <p>Visualizar em:</p>

                            <ul>
                                <li><a href="javascript:;" class="btn verTab" rel="verTab"><span></span>Tabela</a></li>
                                <li><a href="javascript:;" class="btn verLista ativo" rel="verLista"><span></span>Lista</a></li
                            ></ul>
                        </div>
                    </div>
                <div class="clearboth"></div>
            </header>

            <div class="todosImoveis">

             <?php
             if($linhas != '0'){

                $query2 = mysqli_query($conexao,$sql2);
                if(mysqli_num_rows($query2)){


               while ($reg2 = mysqli_fetch_array($query2)) {

               $imovel = $reg2['imovel'];
               $codigo_imovel = $reg2['codimovel'];
               $bairro = $reg2['bairro'];
               $cidade = $reg2['cidade'];
               $uf = $reg2['uf'];
               $quartos = $reg2['quartos'];
               $suites = $reg2['suites'];
               $valor = $reg2['preco'];
               $vagas = $reg2['garagens'];
             ?>    



                <div class='imovel'>
                    <figure><!-- VG Foto: 439978337 -->
                            <a href="<?php echo $url_arquivo; ?>imovel/imovel.php?modulo=<?=$modulo;?>&def=med&codimovel=<?=$reg2['codimovel']?>&form=&tipoPagina=1" title="" >
                                <img src="http://www.imobiliaria.com.br/web/<?php echo $reg2['codimobiliaria'] . '/' . $pasta . $reg2['codimobiliaria'] . '_' .$reg2['codimovel']; ?>_01.jpg" alt="" />
                            </a>
                            <a class='sombra' href='<?php echo $url_arquivo; ?>imovel/imovel.php?modulo=<?=$modulo;?>&def=med&codimovel=<?=$reg2['codimovel']?>&form=&tipoPagina=1'>
                            </a>
                            <a class='geral' href='<?php echo $url_arquivo; ?>imovel/imovel.php?modulo=<?=$modulo;?>&def=med&codimovel=<?=$reg2['codimovel']?>&form=&tipoPagina=1'>
                            </a>
                       </figure>
                       <div class='features'>
                        <h3><?php echo $imovel; ?>
                        </h3>
                            <h4><?php echo $bairro; ?> - <?php echo $cidade ?>/<?php echo $uf ?>
                            </h4>
                                <p class='ref'>Ref.: <?php echo $codigo_imovel; ?>
                                </p>
                                    <div class='importantes in_dorm'>
                                        <span><?php echo $quartos; ?>
                                        </span> dormitório(s)
                                    </div>
                                    <div class='importantes in_suites'>
                                        <span><?php echo $suites; ?>
                                        </span> suíte(s)
                                    </div>
                                    <div class='importantes in_garagens'>
                                        <span><?php echo $vagas; ?>
                                        </span> vaga(s)
                                    </div>
                                    <div class='preco'>
                                        <span><?php echo $oque; ?>
                                        </span>
                                    <div class='clearboth'>
                                    </div> <?php echo 'R$ ' . $valor . ',00'; ?> 
                                    </div>      
                                    <div class='valor'>
                                    </div>              
                                    <div class='favoritos'>
                                        <a href='<?php echo $url_arquivo; ?>impressaod.php?modulo=<?=$modulo?>&def=med&codimovel=<?=$reg2['codimovel']?>&form=&tipoPagina=1' class='thickbox' title='Ficha completa do imóvel'>Ficha do Imóvel</a>

                                        <br>
                                            <a class='maisinfo btn'  href='index.php' >
                                            <img width="30px" height="30px" src="img/voltar.png"/><br>
                                            Voltar
                                            </a>
                                    </div>
                                            <a class='maisinfo btn' href='<?php echo $url_arquivo; ?>imovel/imovel.php?modulo=<?=$modulo;?>&def=med&codimovel=<?=$reg2['codimovel']?>&form=&tipoPagina=1'>Mais informações
                                            </a>    
                            </div>
                       </div>

              <?php 
            }
        }else{
            for ($i=0; $i < 7; $i++) { 
            ?>
            <li>
                <a href="#">
                    <img class="thumbnailDestaque watermark_small" src="images/img-teste.png"  height="90" width="120">
                </a>
            </li>
        <?php 
            }
        }
             }if($linhas == '0'){

                 echo '<script> alert("Não existem imóveis cadastrados dentro desses parâmetros de pesquisa!"); window.opener.location="$url_arquivo"; </script>';

                 }
         ?>                 

            </div><!-- .todosImoveis -->


            <footer>
                <div class="opcoes bottom">
                    <?php /*?><div id="dv_page"><ul class="paginacao">
<li class="pagina"><strong>Página: 1 de 4</strong></li>
<li class="proximo"><a valor="2" href="#">Próximo</a></li>
<li class="ultimo"><a valor="4" href="#">Último</a></li><li class="irpagina">
</ul>
<input type="hidden" id="page" name="page" value="1"><input type="hidden" id="page" name="page" value="1"></div><?php */?>

                </div><!-- .opcoes.bottom -->

                <div class="clearboth"></div>
            </footer>
        </div><!-- .listagemImovel -->

        <div class="clearboth"></div>
    </div><!-- #listImovel -->

    <div class="clearboth"></div>
</div><!-- #pageListagem -->

1 answer

1

Anderson, I don’t like anything php, but sql I understand a little bit... If you made the query by inserting Like + type varíaveis:

select * 
from Tabela
Where Id_Cidade LIKE '%%%'
AND Id_Bairro LIKE '%%%'

I have a similar structure in a project. When the client does not select anything, it passes null or empty string and I do an if and step % to the variable, ie if the client does not select anything picks everything. So sql would look like this:

select * 
from Tabela
Where Id_Cidade LIKE @cidade
AND Id_Bairro LIKE @bairro

Browser other questions tagged

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