How do I echo the three dots “…” after a specific number (e.g., 9) and echo the last two numbers after the three dots “…” (e.g., 57 and 58) for pagination using MySQLi and PHP?
Here’s the code:
<?php
$output = "";
$tpages = ceil($engine->numrows("SELECT null FROM `cms_articles_comments` WHERE `article_id` = '" . $id . "'") / 10);
if ($page == 1)
{
$output .= '<button type="button" class="btn btn-info pull-left goToTop" disabled><i class="fa fa-arrow-left"></i> Previous</button>';
}
else
{
$output .= '<a href="/articles/' . $seo . '&p=' . ($page - 1) . '#comments" ng-click="progress()" class="btn btn-info pull-left goToTop"><i class="fa fa-arrow-left"></i> Previous</a>';
}
if ($page >= $tpages)
{
$output .= '<button type="button" class="btn btn-info pull-right goToTop" disabled>Next <i class="fa fa-arrow-right"></i></button>';
}
else
{
$output .= '<a href="/articles/' . $seo . '&p=' . ($page + 1) . '#comments" ng-click="progress()" class="btn btn-info pull-right goToTop">Next <i class="fa fa-arrow-right"></i></a>';
}
$output .= '<div class="fpagination fpagination-centered"><ul>';
for($i = 1; $i <= $tpages; $i++)
{
if ($i == $page)
{
$output .= '<li class="active"><a href="/articles/' . $seo . '&p=' . $i . '#comments" ng-click="progress()" class="goToTop">' . $i . '</a></li>';
}
else
{
$output .= '<li><a href="/articles/' . $seo . '&p=' . $i . '#comments" ng-click="progress()" class="goToTop">' . $i . '</a></li>';
}
}
$output .= '</ul></div>';
echo $output;
?>
With the code provided above, I get this:
I want the pagination list to show like this:
prntscr.com/a0azua,
prntscr.com/a0ay1s
Thanks in advance.
2
Answers
But again, it’s best to use a framework. Also, if you have less than 9 pages, this creates a problem and you’ll need to add more if/else code for that. In fact, it’s going to look very strange if you have 11 pages because it will be 9 … 10 11, which makes no sense.
The display system you are looking for is more complex than it seems, because if you have 58 pages and your current page is 53, you’ll want to show that three-dot button before page 53 and not after, like this:
And when your current page is 20, you’ll want them to appear on both sides:
In general, you want a system whereby a maximum number of labels/buttons is output (it seems to be 12 in your example), also counting the three-dot labels.
It would be user-friendly to show at least the two pages preceding the current page, and the two following it.
You could write a function to generate a list of page numbers that follow these rules, and use the number 0 to denote the three-dot button:
If you call this function like this:
It will return this:
Or like this, changing the current page number:
It will return this:
Notice the zeroes, which are the place holders for the three-dot buttons.
Now the difficult part has been done. You can call that function from your existing code without having to change much. Instead of your
for
loop, you’ll use aforeach
on the output of the above function. In the loop you’ll have an extra test on the zero, so you can output the three-dot label:Notice that the last argument to the getPageList function is 12. You can configure this number to your needs. It depends on how wide you want your output to become.