How to keep the query filters sent via $ _POST?

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 retrieving the total records of this query.

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

How do I, when I click on the page 2 , 3 , 4 keep the parameters of the current search?

  

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

    
asked by anonymous 10.10.2014 / 17:16

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 GET filter, which is quite 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>';
     }
    
10.10.2014 / 18:55
3

If I understand correctly you lose the values of the query and to keep the values as you go through POST simply load them in input of the query, the example is:

echo "<input name='consulta'";
if($consulta = filter_input(INPUT_POST , 'consulta'))
    echo "value='$consulta'"; 
echo ">";
    
10.10.2014 / 17:34