Reputation: 14992
So, I have this pagination code which breaks a table with pages, it's all working...
But... if there are too many pages it looks ugly, like this:
I'm new into PHP, and this is my current code:
<?php
function pagination($page,$num_page)
{
$max_pages = 15;
echo'<ul class="pagination" style="list-style-type:none; text-align: center;">';
$prevpage = $page-1;
if ($page != 1) {
echo '<li style="padding:5px;"><a href="pesquisa_solicitacao.php?p=1" class="first"></a></li>';
echo '<li style="padding:5px;"><a href="pesquisa_solicitacao.php?p=' . $prevpage . '"><i class="fa fa-chevron-left"></i></a></li>';
}
for($i=1; ($i<=$num_page)/* && ($i<=$max_pages)*/;$i++)
{
if($i==$page)
{
echo'<li style="padding:5px;">'.$i.'</li>';
}
else
{
echo '<li style=" padding:5px;"><a href="pesquisa_solicitacao.php?p=' .$i.'">'.$i.'</a></li>';
}
}
$nextpage = $page+1;
if ($page != $num_page)
{
echo '<li style="padding:5px;"><a href="pesquisa_solicitacao.php?p=' .$nextpage.'"><i class="fa fa-chevron-right"></i></a></li>';
echo '<li style="padding:5px;"><a href="pesquisa_solicitacao.php?p='.$num_page.'" class="last"></a></li>';
}
echo'</ul>';
}
if($num_page>1)
{
pagination($page,$num_page);
}
?>
I just wanted to change it so it looks something like this:
$max_pages=10;
if ($num_pages > $max_pages)
echo "<< < 1 ... 3 4 5 6 7 8 9 10 ... 81 > >>"
How can i do it? I have no idea how to do it in a nice way right now =X But i don't want just a given code, i want to understand how it works and why i should do it like this.
BTW,
$num_pages
is the number of pages that the table will have.
$page
is the current page
$max_pages
would be the maximum number of pages to be shown.
Upvotes: 1
Views: 616
Reputation: 185
<?php
function pagination($page,$num_page)
{
$max_pages = 15;
//Set a start and end point for page links
$start= $page-5;
$end= $page+5;
if($start < 1) $start= 2;
if($end > $num_page) $end= $num_page -1;
//
echo'<ul class="pagination" style="list-style-type:none; text-align: center;">';
$prevpage = $page-1;
if ($page != 1) {
echo '<li style="padding:5px;"><a href="pesquisa_solicitacao.php?p=1" class="first"></a></li>';
echo '<li style="padding:5px;"><a href="pesquisa_solicitacao.php?p=' . $prevpage . '"><i class="fa fa-chevron-left"></i></a></li>';
echo '<li style=" padding:5px;"><a href="pesquisa_solicitacao.php?p=1">1</a></li> $nbsp ... $nbsp ';//Add first page link and ...
}
//Change the starting value of $i to start and end were you want it.
for($i=$start; ($i<=$end)/* && ($i<=$max_pages)*/;$i++)
{
if($i==$page)
{
echo'<li style="padding:5px;">'.$i.'</li>';
}
else
{
echo '<li style=" padding:5px;"><a href="pesquisa_solicitacao.php?p=' .$i.'">'.$i.'</a></li>';
}
}
$nextpage = $page+1;
if ($page != $num_page)
{
echo ' $nbsp ... $nbsp <li style=" padding:5px;"><a href="pesquisa_solicitacao.php?p=' . $num_page . '">' . $num_page . '</a></li>'; //Add last page link and ...
echo '<li style="padding:5px;"><a href="pesquisa_solicitacao.php?p=' .$nextpage.'"><i class="fa fa-chevron-right"></i></a></li>';
echo '<li style="padding:5px;"><a href="pesquisa_solicitacao.php?p='.$num_page.'" class="last"></a></li>';
}
echo'</ul>';
}
if($num_page>1)
{
pagination($page,$num_page);
}
?>
Ok, I added a couple lines, 2 variables, and changed the starting value of i. I commented what i did to explain it. Let me know if you don't understand it.
Upvotes: 1
Reputation: 14992
I changed my code to this and it works quite well now :)
<?php
function pagination($page,$num_page)
{
echo'<ul class="pagination" style="list-style-type:none; text-align: center;">';
$prevpage = $page-1;
if ($page != 1) {
echo '<li style="padding:5px;"><a href="pesquisa_solicitacao.php?p=1" class="first"></a></li>';
echo '<li style="padding:5px;"><a href="pesquisa_solicitacao.php?p=' . $prevpage . '"><i class="fa fa-chevron-left"></i></a></li>';
}
$max_pages = 8; // How many page number you wish to display to the left and right sides of the current page
$index_start = ($page - $max_pages) <= 0 ? 1 : $page - $max_pages;
$index_finish = ($page + $max_pages) >= $num_page ? $num_page : $page + $max_pages;
for ($i = $index_start; $i <= $index_finish; $i++)
{
if ($page == $i)
{
echo'<li style="padding:5px;">'.$i.'</li>';
}
else
{
echo '<li style=" padding:5px;"><a href="pesquisa_solicitacao.php?p=' .$i.'">'.$i.'</a></li>';
}
}
$nextpage = $page+1;
if ($page != $num_page)
{
echo '<li style="padding:5px;"><a href="pesquisa_solicitacao.php?p=' .$nextpage.'"><i class="fa fa-chevron-right"></i></a></li>';
echo '<li style="padding:5px;"><a href="pesquisa_solicitacao.php?p='.$num_page.'" class="last"></a></li>';
}
echo'</ul>';
}
if($num_page>1)
{
pagination($page,$num_page);
}
?>
Upvotes: 0