我该如何回应这三个点“.”在一个特定的数字(例如,9)之后,重复最后两个数字后的三个点“.”(例如,57和58)使用MySQLi和PHP进行分页?
下面是代码:
<?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;
?>通过上面提供的代码,我得到了以下内容:

我希望分页列表显示如下:

prntscr.com/a0azua,prntscr.com/a0ay1s
提前谢谢。
发布于 2016-02-08 07:07:53
您正在寻找的显示系统比看起来要复杂得多,因为如果您有58页,而当前的页面是53页,您需要在第53页之前显示“三点”按钮,而不是在第53页之后显示,如下所示:
1 2 ... 50 51 52 [53] 54 55 56 57 58当您的当前页面为20,您会希望它们出现在两边:
1 2 ... 18 19 [20] 21 22 23 ... 57 58通常,您需要一个系统,其中输出最大数量的标签/按钮(在您的示例中似乎是12 ),还包括三个点标签。
至少显示当前页面之前的两个页面和后面的两个页面将是用户友好的。
您可以编写一个函数来生成符合这些规则的页码列表,并使用数字0表示三个点按钮:
// Returns an array of $max_length (or less) page numbers
// where a 0 in the returned array denotes a gap in the series.
// Parameters:
// $tpages: total number of pages
// $page: current page
// $max_length: maximum size of returned array
function getPageList($tpages, $page, $max_length) {
if ($tpages <= $max_length) {
// no breaks in list
return range(1, $tpages);
}
if ($page <= $max_length - 5) {
// no break on left of page
return array_merge(range(1, $max_length-3), array(0, $tpages-1, $tpages));
}
if ($page >= $tpages - $max_length + 6) {
// no break on right of page
return array_merge(array(1, 2, 0), range($tpages - $max_length + 4, $tpages));
}
// breaks on both sides
$size = $max_length - 6;
$first = $page - floor(($size-1)/2);
return array_merge(array(1, 2, 0), range($first, $first + $size - 1),
array(0, $tpages-1, $tpages));
}如果您像这样调用这个函数:
getPageList(58, 53, 12)它将返回以下内容:
[1, 2, 0, 50, 51, 52, 53, 54, 55, 56, 57, 58]或者像这样,更改当前的页码:
getPageList(58, 20, 12)它将返回以下内容:
[1, 2, 0, 18, 19, 20, 21, 22, 23, 0, 57, 58]请注意零,这是三个点按钮的位置持有人。
现在,困难的部分已经完成。您可以从现有代码中调用该函数,而不必更改太多。您将不使用for循环,而是对上述函数的输出使用foreach。在循环中,您将对0进行额外的测试,这样您就可以输出三点标签:
$output = "";
// first part of your code for getting $tpages and
// generating prev/next buttons comes here: it does not change
// ...
//
$output .= '<div class="fpagination fpagination-centered"><ul>';
foreach (getPageList($tpages, $page, 12) as $i)
{
if ($i == $page)
{
$output .= '<li class="active"><a href="/articles/' . $seo . '&p=' . $i .
'#comments" ng-click="progress()" class="goToTop">' .
$i . '</a></li>';
}
else if ($i == 0)
{
$output .= '<li>…</li>';
}
else
{
$output .= '<li><a href="/articles/' . $seo . '&p=' . $i . '#comments"
ng-click="progress()" class="goToTop">' . $i . '</a></li>';
}
}
$output .= '</ul></div>';
echo $output;注意,getPageList函数的最后一个参数是12。您可以根据需要配置这个编号。这取决于你希望你的输出变得多宽。
发布于 2016-02-07 21:33:42
$bOutputOnce = FALSE;
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>';
} elseif (($i <= 9) or ($i >= ($tpages - 2))) {
$output .= '<li><a href="/articles/' . $seo . '&p=' . $i . '#comments" ng-click="progress()" class="goToTop">' . $i . '</a></li>';
} elseif ($bOutputOnce == FALSE) {
$output .= '<li>…</li>';
$bOutputOnce = TRUE;
}
}但是,最好还是使用一个框架。另外,如果页面不足9页,则会产生问题,需要为此添加更多if / and代码。事实上,如果你有11页的话,它看起来会很奇怪,因为它将是9. 10 11,这是没有意义的。
https://stackoverflow.com/questions/35259078
复制相似问题