Pagination of results

Asked

Viewed 88 times

0

How to insert Pagination of results in the code below, for it only display 5 results in the table and make a counter < 1 2 3 4 > below to show the rest of the result?

<?php header('Content-Type: text/html; charset=iso-8859-1');?>
<?php
 $host = "localhost";
   $user = "root";
   $pass = "";
   $banco = "slp";
   $conexao = mysqli_connect($host, $user, $pass) or die(mysqli_error());
   mysqli_select_db($conexao, $banco) or die(mysqli_error($conexao));

$palavra = $_POST['palavra'];

$sql = mysqli_query($conexao,"SELECT * from cadastros WHERE nomeProfEfetivo LIKE '%".$palavra."%' OR matricula LIKE '%".$palavra."%'");
$row = mysqli_num_rows($sql);
?>
  <section class="panel col-lg-9">

    <header class="panel-heading">
      <br/><br/>

    </header>
    <?php
    if($row>0){
    ?>
      <table class="table table-striped table-advance table-hover">
        <tbody>
          <tr>
            <th><i class="icon_profile"></i> Professor</th>
            <th width="15"><i class="icon_profile"></i> Matricula</th>
            <th><i class="icon_mail_alt"></i> Unidade</th>
            <th width="5"><i class="icon_profile"></i> Modalidade</th>
            <th width="5"><i class="icon_mail_alt"></i> Ano</th>
            <th width="10"><i class="icon_profile"></i> Turma</th>
            <th width="10"><i class="icon_mail_alt"></i> Turno</th>
            <th width=""><i class="icon_mail_alt"></i> Tipo do Prof.</th>
          </tr>
          <?php 
            while($linha = mysqli_fetch_assoc($sql)){
            ?>
          <tr>
            <td width="15">
              <?=$linha['nomeProfEfetivo'];?>
            </td>
            <td width="15">
              <?=$linha['matricula'];?>
            </td>
            <td width="15">
              <?=$linha['unidade'];?>
            </td>
            <td width="15">
              <?=$linha['modalidade'];?>
            </td>
            <td style="text-align:center">
              <?=$linha['ano'];?>
            </td>
            <td style="text-align:center">
              <?=$linha['turma'];?>
            </td>
            <td>
              <?=$linha['turno'];?>
            </td>
            <td style="text-align:center">
              <?=$linha['tipo']; ?>
            </td>




          </tr>

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


      <?php 

    }else{?>
      <h4>Nao foram encontrados registros com esta palavra.</h4>
      <?php }?>
  </section>

1 answer

0

$registros_pagina = "5";
$lista = (int)$_GET["lista"];
if(!$lista) {
   $pc = "1";
}else{
   $pc = $lista;
}
$inicio = $pc - 1;
$inicio = $inicio * $registros_pagina;

//Pega o número total de registros da sua query $sql e coloca na variável  $total
$total = ????
$tp = $total / $registros_pagina;

Your consultation

`......select ........ limit $inicio,$registros_pagina");
 while....... {
   ........
 }`

Paging:

    //altere nome_da_sua_pagina para o nome da sua pagina
    $nomePagina="nome_da_sua_pagina";

    if($total < 1) {
        echo "Nenhum registro encontrado";
    }else{
    if ($total>5){
    echo "<p>";     
    $tp = ceil($tp);
        if($pc>1) {
            $anterior = $pc - 1;
            echo "<a href=\"".$nomePagina.".php?lista=$anterior\">Anterior</a>     ";
        }
        for($i=$pc-5;$i<$pc;$i++) {
            if($i<=0) {
            }else{
                if($i=="$pc") {
                    echo "<b>  $i  </b>";
                }else{
                    echo "  <a href=\"".$nomePagina.".php?lista=$i\">";
                    echo "$i";
                    echo "</a>   ";
                }
            }
        }
        for($i=$pc;$i<=$pc+5;$i++) {
            if($i==$tp) {
                if($i=="$pc") {
                    echo "<b>  $i  </b>";
                }
                else {
                    echo "  <a href=\"".$nomePagina.".php?ista=$i\">";
                    echo "$i";
                    echo "</a>   ";
                }
                break;
            }
            else {
                if($i=="$pc") {
                    echo "<b>  $i  </b>";
                }
                else {
                    echo "   <a href=\"".$nomePagina.".php?lista=$i\">";
                    echo "$i";
                    echo "</a>   ";
                }
                if($i==$pc+5 && $tp>$pc+5) {
                    echo " ...   <a href=\""nomePagina".php?lista=$tp\">$tp</a>";
                }
            }
        }
        if($pc<$tp) {
            $proxima = $pc + 1;
            echo "     <a href=\"".$nomePagina.".php?lista=$proxima\">Próxima</a>";
        }
    echo "</p>";
    }
    }

This "paging" page can be saved in a separate file and be included in the places of your page as follows:

 if ($total!=""){
    include("paginacao.php");
}

Your script would look like this

Obs: with include("paginacao.php");

<?php header('Content-Type: text/html; charset=iso-8859-1');?>
<?php
 $host = "localhost";
   $user = "root";
   $pass = "";
   $banco = "slp";
   $conexao = mysqli_connect($host, $user, $pass) or die(mysqli_error());
   mysqli_select_db($conexao, $banco) or die(mysqli_error($conexao));

$palavra = $_POST['palavra'];

$sql = mysqli_query($conexao,"SELECT * from cadastros WHERE nomeProfEfetivo LIKE '%".$palavra."%' OR matricula LIKE '%".$palavra."%'");
$row = mysqli_num_rows($sql);

$registros_pagina = "5";
$lista = (int)$_GET["lista"];
if(!$lista) {
   $pc = "1";
}else{
   $pc = $lista;
}
$inicio = $pc - 1;
$inicio = $inicio * $registros_pagina;

$tp = $row / $registros_pagina;

$sql = mysqli_query($conexao,"SELECT * from cadastros WHERE nomeProfEfetivo LIKE '%".$palavra."%' OR matricula LIKE '%".$palavra."%' LIMIT $inicio,$registros_pagina");


?>
  <section class="panel col-lg-9">

    <header class="panel-heading">
      <br/><br/>

    </header>
    <?php
    if($row>0){
    ?>
      <table class="table table-striped table-advance table-hover">
        <tbody>
          <tr>
            <th><i class="icon_profile"></i> Professor</th>
            <th width="15"><i class="icon_profile"></i> Matricula</th>
            <th><i class="icon_mail_alt"></i> Unidade</th>
            <th width="5"><i class="icon_profile"></i> Modalidade</th>
            <th width="5"><i class="icon_mail_alt"></i> Ano</th>
            <th width="10"><i class="icon_profile"></i> Turma</th>
            <th width="10"><i class="icon_mail_alt"></i> Turno</th>
            <th width=""><i class="icon_mail_alt"></i> Tipo do Prof.</th>
          </tr>
          <?php 
            while($linha = mysqli_fetch_assoc($sql)){
            ?>
          <tr>
            <td width="15">
              <?=$linha['nomeProfEfetivo'];?>
            </td>
            <td width="15">
              <?=$linha['matricula'];?>
            </td>
            <td width="15">
              <?=$linha['unidade'];?>
            </td>
            <td width="15">
              <?=$linha['modalidade'];?>
            </td>
            <td style="text-align:center">
              <?=$linha['ano'];?>
            </td>
            <td style="text-align:center">
              <?=$linha['turma'];?>
            </td>
            <td>
              <?=$linha['turno'];?>
            </td>
            <td style="text-align:center">
              <?=$linha['tipo']; ?>
            </td>

          </tr>

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

      <?php 

       include("paginacao.php");

    }else{?>
      <h4>Nao foram encontrados registros com esta palavra.</h4>
      <?php }?>
  </section>

Browser other questions tagged

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