I have a page in PHP where I made the pagination that is working perfectly, but I noticed that the screen is getting full of links with page numbering. >
Currently I only present 3 results per page. Could you help me limit the number of paging links ?
I tried to make this limit, but the page always increases by a number to +. I already managed to limit at the beginning, when I move to the next page the number of links increases to infinity!
Follow the code:
<?php
}
//PAGINAÇAO
if ($registros_ > 3) {
//Verificar a pagina anterior e posterior
$max_links = 8;
$links_laterais = ceil($max_links / 2);
$pagina_anterior = $pagina - $links_laterais;
$pagina_posterior = $pagina + $links_laterais;
print "<div class='container'>
<div>
<ul class='pagination'>
<li>";
if ($pagina_anterior > 0) {
print "<a href='pesquisas.php?pagina=$pagina_anterior&tipo=$tipo&seletor=$seletor&filtro=$filtro' aria-label='Previous'>
<span aria-hidden='true'>«</span>
</a>";
} else {
print "<span aria-hidden='true'>«</span>";
}
print "</li>";
//Apresentar a paginacao
for ($i = $pagina_anterior; $i < $pagina_posterior; $i++) {
if ($i >=1){
print"<li><a href='pesquisas.php?pagina=$i&tipo=$tipo&seletor=$seletor&filtro=$filtro'>$i</a></li>";
}
}
print "<li>";
if ($pagina_posterior <= $num_pagina) {
print " <a href='pesquisas.php?pagina=$pagina_posterior&tipo=$tipo&seletor=$seletor&filtro=$filtro' aria-label='Previous'>
<span aria-hidden='true''>»</span>
</a>";
} else {
print "<span aria-hidden='true'>»</span>";
}
print "</div></div>";
}
//F
I was able to solve my problem! Follow the code for those who have the same difficulty.
<?php
}
//PAGINAÇAO
if ($registros_ > 3) {
//Verificar a pagina anterior e posterior
$max_links = 6;
$links_laterais = ceil($max_links / 3);
$pagina_anterior = $pagina - $links_laterais;
$pagina_posterior = $pagina + $links_laterais;
print "<div class='container'>
<div>
<ul class='pagination'>
<li>";
if ($pagina_anterior > 0) {
print "<a href='pesquisas.php?pagina=$pagina_anterior&tipo=$tipo&seletor=$seletor&filtro=$filtro' aria-label='Previous'>
<span aria-hidden='true'>«</span>
</a>";
} else {
print "<span aria-hidden='true'>«</span>";
}
print "</li>";
//Apresentar a paginacao
for ($i = $pagina_anterior; $i < $pagina_posterior; $i++) {
if (($i >=1) && ($i <= $num_pagina)){
print"<li><a href='pesquisas.php?pagina=$i&tipo=$tipo&seletor=$seletor&filtro=$filtro'>$i</a></li>";
}
}
print "<li>";
if ($pagina_posterior <= $num_pagina) {
print " <a href='pesquisas.php?pagina=$pagina_posterior&tipo=$tipo&seletor=$seletor&filtro=$filtro' aria-label='Previous'>
<span aria-hidden='true''>»</span>
</a>";
} else {
print "<span aria-hidden='true'>»</span>";
}
print "</div></div>";
}
//FIM PAGINAÇAO
?>