Perform a PHP filter search on multiple tables

Asked

Viewed 48 times

-4

I need to do a search with filters for example SPECIALTY > CITY and then show search result

follows my code:

                    <?php if ($specialty != null && $city != null) { ?>                                         
                <?php 
        $querySelectSpecialty = "SELECT * FROM specialty WHERE specialtyname = '$specialty'";
        $resultSelectSpecialty = mysqli_query($conn,$querySelectSpecialty);
        $numresults = mysqli_num_rows($resultSelectSpecialty);
        
        if ($numresults >= 1) {
                
                while($rowSelectSpecialty = mysqli_fetch_assoc($resultSelectSpecialty)){
                $id_medico = $rowSelectSpecialty['iddoc'];
                $especialidade = ucfirst($rowSelectSpecialty['specialtyname']);
                $tipo = $rowSelectSpecialty['tipo'];
                    
                $querySelectLocation = "SELECT * FROM endereco_doc WHERE city = '$city'";
                $resultSelectLocation = mysqli_query($conn,$querySelectLocation);
                
                while($rowSelectLocation = mysqli_fetch_array($resultSelectLocation)){
                $id_medico_location = $rowSelectLocation['iddoc'];
                $cep = $rowSelectLocation['cep'];
                $rua = $rowSelectLocation['rua'];
                $numero = $rowSelectLocation['numero'];
                $bairro = $rowSelectLocation['bairro'];
                $complemento = $rowSelectLocation['complemento'];
                $cidade = $rowSelectLocation['city'];
                $uf = $rowSelectLocation['uf'];
                $id_medico = $rowSelectLocation['iddoc'];
                
                
                $querySelectProfile = "SELECT * FROM doc WHERE iddoc = '$id_medico_location'";
                $resultSelectProfile = mysqli_query($conn,$querySelectProfile);
                $numResultProfile = mysqli_num_rows($resultSelectProfile);
                
                while($rowSelectProfile = mysqli_fetch_array($resultSelectProfile)){
                $nome = $rowSelectProfile['nome'];
                $sobrenome = $rowSelectProfile['sobrenome'];
                $n_crm = $rowSelectProfile['n_crm'];
                $uf_crm = $rowSelectProfile['uf_crm'];
                
                 ?>
                <div class="result-search-box">
                    <div class="result-search-header">
                        <div class="image">
                            <div class="profile-image-filter" style="background-image:url('http://localhost/images/no-image-profile.jpg');"></div>
                        </div>
                        <div class="title">
                            <h3><span id="first-name-filter"><?php echo "$nome"; ?></span><span> </span><span id="last-name-filter"><?php echo "$sobrenome"; ?></span></h3>
                            <p><span id="specialty-filter"><?php echo "$especialidade"; ?></span><span> / CRM </span><span id"crm-number-filter"><?php echo "$n_crm"; ?></span><span> </span><span id="crm-uf-filter"><?php echo "$uf_crm"; ?></span></p>
                            <span id="avaliations">
                                <img id="avaliation-stars" src="http://localhost/images/no-stars.png">
                                <a id="profile-result-avaliation-link" href="#"><span><span id="number-avaliations">0</span> opiniões</span></a>
                            </span>
                        </div>
                    </div>

                    <div class="profile-address">
                        <div class="profile-address icon">
                            <span id="title-city"><?php echo "$cidade"; ?></span><br>
                            <div id="address-full">
                                <span id="address"><?php echo "$rua"; ?></span><span>, </span><span id="addressnumber"><?php echo "$numero"; ?></span><span>, </span><span id="complement"><?php echo "$complemento"; ?></span><span> - </span><span id="district"><?php echo "$bairro"; ?></span><span>, </span><span id="city"><?php echo "$cidade"; ?></span><span> - </span><span id="uf"><?php echo "$uf"; ?></span>
                            </div>
                        </div>
                        <div class="profile-address-contact">
                            <div class="profile-whatsapp btn">
                                <a class="btnlight" id="profile-search-result" href="#" target="_blank" style="text-decoration:none;"><button id="btn-profile-search-result">Ver perfil</button></a>
                                <a id="whatsapp-profile-search" href="https://api.whatsapp.com/send?phone=554731838200&text=Ol%C3%A1" target="_blank" style="text-decoration:none;"><button id="btn-whatsapp-contact">Chamar no Whatsapp</button></a>
                            </div>                              
                        </div>
                    </div>
                </div>

                <?php 
                  } // while resultSelectProfile
                  } // while resultSelectProfile
                } // while resultSelectSpecialty
                  
                  } else {
                    
                    echo "Nada encontrado nos parâmetros informados.";
                    
                }
                
        } else {
            
            echo "Obrigatório indicar a Especialidade e Cidade.";
            
        }
                ?>

It turns out that it is returning several results and multiplying them, the correct one was to appear only the results according to the filters and without multiplication of data returned

It is a while inside another, for one calls a select from the database

How can I solve?

  • Carlos can you update your question with an examples of the data you have in each table please? and some print? It will be easier to help you.

  • It seems that you are seeking several times the same value of iddoc. Like $specialty and $city cannot be null to start the search, you can delete half those while within while. SELECT * FROM specialty, endereco_doc WHERE specialty.specialtyname = '{$specialty}' AND endereco_doc.city = '{$city}'

  • Junior, you helped me, I needed this, thank you very much! solved.

  • Not at all, Carlos. I will post as an answer and mark as accepted, please.

1 answer

-1


It seems that you are seeking several times the same value of iddoc.

Like $specialty and $city cannot be null to start the search, you can elimitar half those while inside while.

Ex: if there are 200 records, it will run the internal loop 200 times, getting lousy in terms of performance.

Note that in the SELECT I used AND, which means that the two conditions need to be met. Otherwise, you can use OR, to be attended to.

There are other ways to query two tables at the same time, but I believe this is simpler by your code. Try:

SELECT * FROM specialty, endereco_doc WHERE specialty.specialtyname = '{$specialty}' AND endereco_doc.city = '{$city}'
  • Solved! Thanks'

  • In case I want to make pagination with these results, it is possible with several whiles?

  • Click the arrow on the left to mark the answer as accepted. Yes, it is possible for a page researcher, even with whiles it is not complicated. If you encounter problems, just open a new question with doubts.

  • done, please vote on my question , are putting -3 and nobody helps me, this is preventing to create new questions

  • Voted. You can use a resource to create JS pagination. See https://datatables.net/

Browser other questions tagged

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