Problem with PHP pagination

-1

I have a problem with pagination, I have the main page where the whole structure of my site, called main.php, is located. There you have the switch to open the files, that way.

PAGE MAIN.PHP

switch(@ $_GET['pag'])
      { case "historico": include("historico.php"); break; ...

But the problem is in the page historico.php, where I have there a form action with an input of name plate, where the person types the plate and appears the data, until then everything OK, I made the paging code, however when I click on the next page the input appears again, so I type the card, after I type the card that appears the page that I clicked, being that the correct one was to load the next page without having to retype in the input. Following history code.php

HISTORICAL PAGE.PHP

<form action="" method="post">
             <table border="0" cellpadding="0" cellspacing="0"  id="id-form">  
          <tr>
                <th valign="top">DIGITE A PLACA:</th>
                    <td>
                    <input name="placa" type="text" class="inp-form" autofocus placeholder="Digite Aqui" size="7"  required maxlength="7" onkeyup="toUpper(this);" />
                    </td>
                <td>
        <div class="error-left"></div>
        <div class="error-inner">Este campo é obrigatório.</div>
        </td>
          </tr>
              </table>
 </form>
 <?php
 if(isset($_POST['placa'])){
 $placa = $_POST['placa'];
 $_SESSION['placa'] = $placa;
 $query = mysql_query("SELECT * FROM veiculos WHERE placa = '$placa' ");
 $dados = mysql_fetch_array($query);
 $busca = mysql_query("SELECT * FROM horarios WHERE placa = '$placa' ");

 if(mysql_num_rows($query) == 0){
echo "  
    <div id=message-red><br>
            <table border=0 width=100% cellpadding=0 cellspacing=0>
            <tr>
                <td class=red-left>Error. <a href=>Placa não existe no banco de dados, verifique se não há necessidade de cadastro do veículo.</a></td>
                <td class=red-right><a class=close-red><img src=images/table/icon_close_red.gif   alt= /></a></td>
            </tr>
            </table>
            </div></p>";
    } // fim do mysql_num_rows($query) == 0

   elseif(mysql_num_rows($busca) == 0) {
    echo "
    <div id=message-red><br>
            <table border=0 width=100% cellpadding=0 cellspacing=0>
            <tr>
                <td class=red-left>Error. <a href=>A placa informada ainda não possui registros de entrada e saída.</a></td>
                <td class=red-right><a class=close-red><img src=images/table/icon_close_red.gif   alt= /></a></td>
            </tr>
            </table>
            </div></p>"; }
 else {?>

  <form method='post'>
  <table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
            <tr>
                <th class="table-header-cabecalho line-left minwidth-1">Placa do Veículo</th>
            <th class="table-header-cabecalho line-left minwidth-1">Nome do Motorista</th>
            <th class="table-header-cabecalho line-left minwidth-1">RG</th>
            <th class="table-header-cabecalho line-left minwidth-1">Empresa</th>
            <th class="table-header-cabecalho line-left minwidth-1">Entrada</th>
            <th class="table-header-cabecalho line-left minwidth-1">Saída</th>
            <th class="table-header-cabecalho line-left minwidth-1">Nota</th>
            </tr>
           <?php
        $limite = 5;
        $pagina = @$_GET['pagina'];
        if(!$pagina){
            $pagina = 1;
        }
        $inicio = ($pagina * $limite) - $limite;
        $linhas = "SELECT veiculos.placa, veiculos.nome_motorista, veiculos.rg, veiculos.empresa, horarios.obs, horarios.entrada, horarios.saida FROM veiculos INNER JOIN horarios ON veiculos.placa=horarios.placa where veiculos.placa = '$placa' ORDER BY horarios.id_hor DESC limit $inicio,$limite";
        $query = mysql_query($linhas);

        while($linhas = mysql_fetch_array($query)){
            echo "<tr>";
            echo "<td>".$linhas['placa']."</td>";
            echo "<td>".$linhas['nome_motorista']."</td>";
            echo "<td>".$linhas['rg']."</td>";
            echo "<td>".$linhas['empresa']."</td>";
            echo "<td>". date("d/m/Y - H:i:s",strtotime($linhas['entrada']))."</td>";
            if($linhas['saida'] == 0){echo "<td><font size='+3' color='#CC0000'>Veículo na empresa</font></td>";}else { echo "<td>". date("d/m/Y - H:i:s",strtotime($linhas['saida']))."</td>";}
            echo "<td>".$linhas['obs']."</td>";
            echo "</tr>";
        }
        $total_registros = mysql_num_rows(mysql_query("SELECT veiculos.placa, veiculos.nome_motorista, veiculos.rg, veiculos.empresa, horarios.obs, horarios.entrada, horarios.saida FROM veiculos INNER JOIN horarios ON veiculos.placa=horarios.placa where veiculos.placa = '$placa'"));
        $total_paginas = ceil($total_registros / $limite);
            //exibe a paginação
               for($i = 1; $i <=$total_paginas; $i++) {

               echo "
                <table border='0' cellpadding='0' cellspacing='0' id='paging-table'>
                <tr>
                <td>
                          <a href='?pag=historico&&pagina=$i'>".$i."</a>
                </td>
                </tr>
                </table>";
           }

             ?>
 </table>
 </form>
        <!--  end paging................ -->
        <div class="clear"></div>
       <?php    
  } // fim do else $dados
 } // fim do isset do $_POST
 ?>

Did you understand? lol, I'm waiting for help.

    
asked by anonymous 30.07.2014 / 03:18

1 answer

1

I believe the error is at the beginning of the code when you do the validation if form (post) was sent:

if(isset($_POST['placa'])){

By clicking to the next page you are sending a GET with "pag" and "page", so it will not pass validation and consequently will not display anything. There are two ways to validate and display the records as desired:

1 - Validating the session you created

if(isset($_POST['placa']) || (isset($_SESSION['placa']) && !is_null($_SESSION['placa']))){

2 - Validating the page parameter

if(isset($_POST['placa']) || (isset($_GET['pagina']) && is_numeric($_GET['pagina']))){

That's it, I hope it helps!

Big hug

    
30.07.2014 / 20:38