I am working with PHP and MYSQL, I have a list showing all requests entered. The first column is ID and shows the number of the request. I have a code for pagination and show 10 records per page. The problem I have is that on the first page the ID works fine and shows from 1 to 10, but when I enter the second page it should start with 11, but the counter resets and starts from 1 again. I share my code, hoping someone can help me solve this.
I use count_id to display the ID number
function count_id()
{
static $count = 1;
return $count++;
}
$sql_register = $db->query("SELECT COUNT(*) as total_registro FROM pasantia");
$result_register = mysqli_fetch_array($sql_register);
$total_registro = $result_register['total_registro'];
$solicitudesPorPagina = 10;
$pagina = 1;
if (isset($_GET["pagina"]))
{
$pagina = $_GET["pagina"];
}
$limit = $solicitudesPorPagina;
$offset = ($pagina - 1) * $solicitudesPorPagina;
$paginas = ceil($total_registro / $solicitudesPorPagina);
$query = $db->query("SELECT
p.id,p.fecha_solicitud,p.nombre_docente,p.rut,p.email,p.telefono,p.titulo,p.fecha_inicio,
p.fecha_termino,p.registro_sis,e.name AS
nombre_establecimiento,c.nombre_carr AS nombre_carrera
FROM pasantia p INNER JOIN carr c ON c.id = p.carrera INNER JOIN
establecimiento e ON e.id = p.campo_formador
ORDER BY p.fecha_solicitud DESC LIMIT $offset,
$solicitudesPorPagina");
}
$result = mysqli_num_rows($query);
if($result > 0)
{
while ($data = mysqli_fetch_array($query))
{
?>
<tr>
<td class="text-center"><?php echo count_id();?></td>
<td class="text-center"> <?php echo read_date($data['fecha_solicitud']); ?></td>
<td class="text-center" > <?php echo remove_junk($data['nombre_docente']); ?></td>
<td class="text-center"> <?php echo remove_junk($data['rut']); ?></td>
<td class="text-center"> <?php echo remove_junk($data['nombre_carrera']); ?></td>
<td class="text-center"> <?php echo remove_junk($data['nombre_establecimiento']);
?></td>
<td class="text-center"> <?php echo date('d-m-Y',
strtotime($data['fecha_inicio'])); ?></td>
<td class="text-center"> <?php echo date('d-m-Y',
strtotime($data['fecha_termino'])); ?></td>
<td class="text-center">
<div class="btn-group">
<a href="editar_pasantia.php?id=<?php echo (int)$data['id'];?>" class="btn
btn-info btn-xs" title="Editar" data-toggle="tooltip">
<span class="glyphicon glyphicon-edit"></span>
</a>
<a href="eliminar_pasantia.php?id=<?php echo (int)$data['id'];?>" class="btn
btn-danger btn-xs" title="Eliminar" data-toggle="tooltip">
<span class="glyphicon glyphicon-trash"></span>
</a>
</div>
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
////// PAGINACION //////
<nav aria-label="Page navigation example">
<div class="row">
<div class="col-xs-12 col-sm-6">
<p>Mostrando <?php echo $solicitudesPorPagina ?> de <?php echo $total_registro ?>
solicitudes disponibles</p>
</div>
<div class="col-xs-12 col-sm-6">
<p>Página <?php echo $pagina ?> de <?php echo $paginas ?> </p>
</div>
</div>
<ul class="pagination justify-content-right">
<!-- Si la página actual es mayor a uno, mostramos el botón para ir una página atrás -->
<?php if ($pagina > 1) { ?>
<li>
<a href="./pasantia.php?pagina=<?php echo $pagina - 1 ?>">
<span aria-hidden="true">«</span>
</a>
</li>
<?php } ?>
<!-- Mostramos enlaces para ir a todas las páginas. Es un simple ciclo for-->
<?php for ($x = 1; $x <= $paginas; $x++) { ?>
<li class="<?php if ($x == $pagina) echo "active" ?>">
<a href="./pasantia.php?pagina=<?php echo $x ?>">
<?php echo $x ?></a>
</li>
<?php } ?>
<!-- Si la página actual es menor al total de páginas, mostramos un botón para ir una
página adelante -->
<?php if ($pagina < $paginas) { ?>
<li>
<a href="./pasantia.php?pagina=<?php echo $pagina + 1 ?>">
<span aria-hidden="true">»</span>
</a>
</li>
<?php } ?>
</ul>
</nav>
You could just add to it
$offset
when printingcount_id()
.Example: