Errors with pagination and table filter in js

0

I found some practical errors running my code and I do not know how to fix it. Here is the code below:

<div id="divConteudo">
  <?php 

  $con_string = "host='***' port=5432 dbname='***' user='***' password='***'";
  $conn = pg_connect($con_string);
  $query = "select pa_maquina, pa_hora - pa_inicioparada as tempo, pa_motivo, pa_data, pa_observacao from relatorioparadas";


 $total_reg = "10"; // número de registros por página


 $pagina=$_GET['pagina'];
 "'?pagina='1'";
 if (!$pagina) {
 $pc = "1";
 } else {
 $pc = $pagina;
 }

 $inicio = $pc - 1;
 $inicio = $inicio * $total_reg;
 $limite = "$query LIMIT $total_reg offset $inicio";
 $todos = pg_query("$query");
 $tr = pg_num_rows($todos); // verifica o número total de registros
 $tp = $tr / $total_reg; // verifica o número total de páginas

 $anterior = $pc -1;
 $proximo = $pc +1;

 $output=pg_query($conn,$limite);

  { ?> 
  <table id="tabela">
        <thead>
           <tr>

                <th><center>Máquina</center></th>
                <th><center>Tempo de Parada</center></th>
                <th><center>Motivo</center></th>
                <th><center>Observações de Ocorrencia</center></th>
                <th><center>Data</center></th>

            </tr>
            <tr>

                <th><center><input type="text" id="txtColuna1"/></center></th>
                <th><center><input type="text" id="txtColuna2"/></center></th>
                <th><center><input type="text" id="txtColuna3"/></center></th>
                <th><center><input type="text" id="txtColuna4"/></center></th>
                <th><center><input type="text" id="txtColuna5"/></center></th>
            </tr>         
      </thead>
        <tbody>

          <tr>
          <?php

          while ($result = pg_fetch_array($output)) {
          $maquina = $result["pa_maquina"];
          $tempo = $result["tempo"];
          $motivo = $result["pa_motivo"];
          $data = $result["pa_data"];
          $observacao = $result["pa_observacao"];




              echo '<td>'; echo $result["pa_maquina"]; echo'</td>';
                 echo '<td>'; echo $result["tempo"]; echo'</td>';
                 echo '<td>'; echo $result["pa_motivo"]; echo'</td>';
                echo '<td>'; echo $result["pa_observacao"]; echo'</td>';
                 echo '<td>'; echo $result["pa_data"]; echo'</td>';

            echo'</tr>


       </tbody>'; }?>
    </table> 
    <?php } ?>


 </div>

     <div class="row x_title text-right">

     <input  type= "submit" id="visualizarDados" value="OK"> 
     <?php
    if ($pc>1) {
echo "<a href='?pagina=$anterior'><button><- Anterior</button></a> ";
}

    if ($pc<$tp) {
echo " <a href='?pagina=$proximo'><button>Próxima -></button></a>";
}

?>

     </div>


<script>
 $(function(){
 $("#tabela input").keyup(function(){       
    var index = $(this).parent().index();
    var nth = "#tabela td:nth-child("+(index+1).toString()+")";
    var valor = $(this).val().toUpperCase();
    $("#tabela tbody tr").show();
    $(nth).each(function(){
        if($(this).text().toUpperCase().indexOf(valor) < 0){
            $(this).parent().hide();
        }
    });
 });

 $("#tabela input").blur(function(){
    $(this).val("");
 });
 });



 </script>

First error: loading the page says that the page variable was not defined, even with that "if" just below. When I click next, the error disappears. Second error: The filter is only working for the first column, when it should filter the column where it is being typed. Here is an image of the table:

ThefiltercodeIobservedfromthistutorial: link

I am a beginner in the field and I accept any suggestions.

    
asked by anonymous 13.09.2018 / 14:24

1 answer

0

The alert is being caused by the line

  

$ page = $ _ GET ['page'];

where you are assigning the value of $ _GET ['page'] directly. Try to put an if to check if it exists. Ex:

if( isset( $_GET['pagina'] ) && !empty( $_GET['pagina'] ) ){
 $pagina = $_GET['pagina'];
}else{
 $pagina = 1;
}
    
17.09.2018 / 16:38