Problem with PHP paging

Asked

Viewed 412 times

-1

I’m having problems with paging, I have the main page where you can find the whole structure of my site, called main.php where there is the switch to open the files, so.

MAIN PAGE.PHP

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

But the problem is in the historical page.php, where if I have there a form action with an input card name, where the person type the board and appears the data, until then everything OK, I made the pagination code, but when I click on the next page appears the input again, for me to enter the plate, after I type the plate that appears the page I clicked, being that the correct one was to load the next page without the need to type again in the input. Follows code from the history.php

PAGE HISTORY.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
 ?>

Do you understand? I’m waiting for help.

  • Someone who can help me?

  • It’s time to use PDO and better separate this code, at least views to one side and models to the other, your code is well mixed, it is difficult to maintain

  • You can pass the board through the URL or save in session.. being a query parameter, I would use the URL

  • And take that off @$_GET... use isset to verify the variable

1 answer

1

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

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

By clicking on the next page you are sending a GET with "pag" and "page", so you will not pass the validation and consequently not display anything. There are two ways to validate and display 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

  • is_numeric will accept hexadecimals as '0xFF'. Pagination is (0-9) and any other argument should be treated as zero or a - start page.

  • @Mastria , I did as I said, I changed if(isset($_POST['placa'])){ by example 1 and returned the error Notice: Undefined index: placa in... line 45 when I click on the page.

  • 1

    @Paulopimentel after the validation, assign the value to the variable $placa also doing validation: $placa = (isset($_SESSION['plate'])) ? $_SESSION['plaque'] : $_POST['plaque'];

  • $placa = (isset($_SESSION['placa'])) ? $_SESSION['placa'] : $_POST['placa']; assign value to variable NO is validation

  • @Mastria, it worked, but when I open the historical page.php the data is already open, the pages are opening correctly but the imput I can not search any board.

  • @Paulopimentel give me your email or skype, so I help you better! Hug

  • Add my skype: paulinhooficial or email: [email protected]

  • The OS itself has a chat if you want to use

Show 3 more comments

Browser other questions tagged

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