PHP and Mysql pagination

Asked

Viewed 153 times

1

I created a search page with two search fields, it’s working and everything ok, only when I research I would like to page, and I’m doing something wrong.

My code.

<?php

  $conexao =mysqli_connect($host,$user,$pass,$bank);
  $pagina = (isset($_GET['pagina'])) ? (int)$_GET['pagina'] : 1 ;


  $consulta1 ="SELECT * FROM registro_clientes";
  $qrTotal  =mysqli_query($conexao,$consulta1) or die(mysqli_error());
  $numTotal =mysqli_num_rows($qrTotal);
  $quantidade = 2;
  $inicio = ($pagina - 1) * $quantidade;
  $totalPagina=ceil($numTotal/$quantidade);




   if(mysqli_connect_errno($conexao)){
       echo "erro conect hen";
       }else{

             {

               $nome = $_GET['buscar'];
               $bairro = $_GET['onde'];
           $consulta = "SELECT * FROM registro_clientes 
           WHERE nome_cliente LIKE '%$nome%' AND bairro LIKE '%$bairro%' 
           ORDER BY preferencial ASC 
           LIMIT $inicio, $quantidade ";
           $executar = mysqli_query($conexao,$consulta);

           $total = mysqli_num_rows($executar);

        echo  "Encontrado     ".$total. "     resultado(s):";
        echo "<br>";
        echo "<hr>";                        

         while ($ln=mysqli_fetch_array($executar)){              
        echo "<br>";
        echo "" .$ln['nome_cliente'] ."<br>";
        echo "" .$ln['nome_comercio'] ."<br>";
        echo "" .$ln['tipo_com_serv'] ."<br>";
        echo "" .$ln['bairro'] ."<br>";
        echo "" .$ln['endereco'] ."<br>";
        echo "" .$ln['fone1'] ."<br>";
        echo "" .$ln['email'] ."<br>";
        echo "" .$ln['site'] ."<br>";
        echo"" .$ln['nome_imagem'] ."<br>";                     
        echo "<br>";
        echo "<hr>";                        
                    }                               
                }
          }  


       echo '<a href="?pagina=1">  primeira pag  </a> - ';

       for($i = 1; $i <= $totalPagina; $i++){

           if($i == $pagina)
            echo $i;
            else
            echo"<a href='resultados.php?pagina=$i'>". $i. "</a>";  

           }

       echo" - <a href=\"?pagina=$totalPagina\">  ultima pag  </a> ";





           ?>
  • What mistake is going on?

  • this way it presents itself, it does the search and the pagination,only it still presents all the results taking the following pages, the results that have not been filtered, basically it plays all the data in the next pages

  • explaining better, I have 6 record in the database, searching the fields I want a record, it filters me this and plays the result, only that the paginacao still exists, if I click on page 2 after page 3 or press page 1 it throws me all the results that I have in my database, I don’t know or finding how to do it correctly, that I search, and only me page if there is need.

  • @Henriqueoliveira, put your explanation in your question please

  • From the obtained result it seems to me that the problem is in the variables, $_GET['fetch'] and $_GET['where'], probably when you click on the pagination the variables caught by GET

  • so I had a line above them so if(isset($_GET['search'])&& isset($_GET['where'])) with this line the search works, but the paging still persists wrong, I saw now that this line was missing in the code I posted, I’m testing here many things in vain.

Show 1 more comment
No answers

Browser other questions tagged

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