How to keep query filters sent via $_POST?

Asked

Viewed 737 times

0

$page = ((isset($_GET['pagina']) && intval($_GET['pagina']) > 1)? intval($_GET['pagina']) : 1) - 1;
$limite = 12;
$atual = $page * $limite;
$limit = " LIMIT {$atual}, {$limite}";

// Registros limitados
$query    = $pdo->query("SELECT * FROM imovel ".trim($where, ' AND ').$limit); 
$total    = $pdo->query("SELECT * FROM imovel ".trim($where, ' AND '));
$total2   = $total->rowCount();
$qtdpage  = ceil($total2/$limite); // Quantidade de páginas

for ($i = 1; $i < $qtdpage; $i++){
     echo '<li><a href="busca?pagina='.$i.'">'.$i.'</a></li>';
}

With this code I am retrieving the records of my query and recovering the total records of that query.

Filter variables are being sent via POST and when I change page type of the 1 to the 2, the filter is lost, meaning it understands that no parameters have been passed and then applies the default search.

Like, by clicking on the page 2, 3, 4 keep the current search parameters?

This is what happens when I move to page 2 or any other.

inserir a descrição da imagem aqui

2 answers

4


Just as @Bacco commented, you can have multiple filters on different tabs, that is, using the same session, you can use the filter via GET, which is very plausible.

<?php
   $filtro = '';
   if (isset($_REQUEST['imovel'])){ // REQUEST busca os dados tanto de POST como de GET
     $imovel = $_REQUEST['imovel']; // Trate o SQL Injection
     $where = " imovel = {$imovel} AND "
     $filtro .= '&imovel='.urlencode($imovel);
   }

     $page = ((isset($_GET['pagina']) && intval($_GET['pagina']) > 1)? intval($_GET['pagina']) : 1) - 1;
     $limite = 12;
     $atual = $page * $limite;
     $limit = " LIMIT {$atual}, {$limite}";



     // Registros limitados
     $query    = $pdo->query("SELECT * FROM imovel ".trim($where, ' AND ').$limit); 
     $total    = $pdo->query("SELECT * FROM imovel ".trim($where, ' AND '));
     $total2   = $total->rowCount();
     $qtdpage  = ceil($total2/$limite); // Quantidade de páginas

     for ($i = 1; $i < $qtdpage; $i++){
          echo '<li><a href="busca?pagina='.$i.$filtro.'">'.$i.'</a></li>';
     }

Or store query data in sessions:

<?php
   session_start();
   if (isset($_POST['imovel'])){
     $_SESSION['filtro'] = Array();
     // Não se esquça de fazer tratamento para SQL Injection
     $_SESSION['filtro']['imovel'] = $_POST['imovel']; 
   }

     $page = ((isset($_GET['pagina']) && intval($_GET['pagina']) > 1)? intval($_GET['pagina']) : 1) - 1;
     $limite = 12;
     $atual = $page * $limite;
     $limit = " LIMIT {$atual}, {$limite}";

     if (isset($_SESSION['filtro']['imovel'])){
        $where = " imovel = {$_SESSION['filtro']['imovel']} AND "
     }

     // Registros limitados
     $query    = $pdo->query("SELECT * FROM imovel ".trim($where, ' AND ').$limit); 
     $total    = $pdo->query("SELECT * FROM imovel ".trim($where, ' AND '));
     $total2   = $total->rowCount();
     $qtdpage  = ceil($total2/$limite); // Quantidade de páginas

     for ($i = 1; $i < $qtdpage; $i++){
          echo '<li><a href="busca?pagina='.$i.'">'.$i.'</a></li>';
     }

3

If I understand correctly you lose the values of the consultation and to maintain the values as you go through POST just press the same on input of the consultation, is the example:

echo "<input name='consulta'";
if($consulta = filter_input(INPUT_POST , 'consulta'))
    echo "value='$consulta'"; 
echo ">";
  • My pagination doesn’t make a query, it just tries to switch to the page 2 since I already loaded everything I needed on the first consultation.

  • I don’t understand your problem so...

Browser other questions tagged

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