Paging within a div

Asked

Viewed 149 times

2

I’m creating pagination inside a div and I intend that from page to page returns 5 in 5 lines, but it’s not working, when I click to open the second page does not return 5 different lines, only changes the first and last as shown in the image:

in this image returns the first 5 lines: inserir a descrição da imagem aqui

this image returns when I click to open page 2, where it only changes from the previous line and removes the first line from the previous page: inserir a descrição da imagem aqui

Code:

<?php
require("conexao.php");
require("init.php");

$itens_por_pagina1 = 5;

$pagina1 = intval($_GET['pagina1']);

$query1 = "SELECT * FROM raddb.Grupo ORDER BY Discricao ASC LIMIT $pagina1, $itens_por_pagina1";

$result1 = $conn->query($query1) or die($conn->error);

$produto1 = $result1->fetch_assoc();

$num1 = $result1->num_rows;

$num_total1 = $conn->query("SELECT * FROM raddb.Grupo ORDER BY Discricao ASC")->num_rows;

$num_paginas1 = ceil($num_total1/$itens_por_pagina1);

<div class="table-responsive" id="employee_table1">  
<?php if($num1 > 0){ ?> 
<table class="table table-hover table-striped lista-clientes1">  
<legend><b>Grupos</b></legend>
<tr> 
<span class="glyphicon glyphicon-filter" data-toggle="modal" data-original-title="Filter"></span><input type="text" class="input-search1" alt="lista-clientes1"/>
<th width="25%">Grupo</th>
<th width="30%">Ações</th>                                  
</tr>  
<?php  do{ ?> 
<tr>  
<td><?php echo $produto1["Discricao"]; ?></td>  
<td><button type="button" name="view1" id="<?php echo $produto1["Id"]; ?>" data-toggle="modal" href="#dataModal1" class="btn btn-warning btn-sm view_data1" /><span class="glyphicon glyphicon-edit"></span></button></td>
</tr>  
<?php } while($produto1 = $result1->fetch_assoc()); ?> 
</table> 
<nav>
<ul class="pagination">
<li>
<a href="index.php/novoutilizador?pagina1=0" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php 
for($i=0;$i<$num_paginas1;$i++){
$estilo = "";
if($pagina1 == $i)
$estilo = "class=\"active\"";
?>
<li <?php echo $estilo; ?> ><a href="index.php/novoutilizador?pagina1=<?php echo $i; ?>"><?php echo $i+1; ?></a></li>
<?php } ?>
<li>
<a href="index.php/novoutilizador?pagina1=<?php echo $num_paginas1-1; ?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
<?php } ?>
</div> 
</div> 

1 answer

2


In your database query, after LIMIT you have two parameters, the first is the starting position, the second is the amount of records that will be returned. The problem is in the first parameter, you need to change when the page is exchanged, multiplying the page number by the total of page records. Here is an example of paging:

<?php
$conn = mysql_connect("host","usuario","senha");
$db = mysql_select_db("bancodedados");
$busca = "SELECT * FROM tabelax";
$total_reg = "10"; // número de registros por página

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

$inicio = $pc - 1;
$inicio = $inicio * $total_reg;
$limite = mysql_query("$busca LIMIT $inicio,$total_reg");
$todos = mysql_query("$busca");
 
$tr = mysql_num_rows($todos); // verifica o número total de registros
$tp = $tr / $total_reg; // verifica o número total de páginas
 

while ($dados = mysql_fetch_array($limite)) {
	$nome = $dados["nome"];
	echo "Nome: $nome<br>";
}
 
// Navegação
$anterior = $pc -1;
$proximo = $pc +1;
if ($pc>1) {
echo " <a href='?pagina=$anterior'><- Anterior</a> ";
}
echo "|";
if ($pc<$tp) {
echo " <a href='?pagina=$proximo'>Próxima -></a>";
}
?>

Browser other questions tagged

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