0

目前我有一个有效的分页脚本,虽然我缺少功能。目前,$pages 列表中可能会显示数百个页面,因为在 [1] [...] 5, 6, 7 [..] [45] 之间没有可以显示的过滤器. 这是我的代码:

    /** Pagination **/
    $limit = 7;
    $query = "SELECT COUNT(*) FROM users";
    $result = $db->prepare($query);
    $result->execute();
    $pages_query = $result->fetchColumn(0);
        $count = number_format($pages_query);
        $pages = ceil($pages_query / $limit);
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $start = ($page - 1) * $limit;
    ob_start();
        echo("<span class='alignright'>");
        if ($pages >= 1 && $page <= $pages){
            if($page > 1){
                $next = ($page - 1);
                $link = "?page=$next";
                echo("<a class='paginate' href='$link'><i class='icon-caret-left'></i></a>");
            }
        for ($x=1; $x<=$pages; $x++){
            echo ($x == $page) ? "<strong style='font-weight: bold!important;'><a class='paginate' href='?page=$x'>$x</a></strong>" : "<a class='paginate' href='?page=$x'>$x</a>";
        }
        if($page < $pages){
            $next = ($page + 1);
            $link = "?page=$next";
            echo("<a class='paginate' href='$link'><i class='icon-caret-right'></i></a>");
        }
        echo("</span>");
        if($count > 0){
            echo("<span class='smalltext'>Page <strong class='half'>$page</strong> of $pages:</span>");
        } else {
            echo("<span class='smalltext'>There are <span class='half'>$count</span> results to display.</span>");
        }
    $pagintion = ob_get_clean();

(它已从其中的其他垃圾中剥离,但这是一般框架。)基本上,我试图弄清楚如何将其限制为问题顶部指定的底部“页面之间”。类似的东西:[<] [1] ... [4] [5] [6] ... [45] [>] 如果这是有道理的。

4

3 回答 3

0

我个人对分页的含义是它必须易于阅读并且以后可以轻松更改。我根据您的示例键入了以下代码:

$totalPages = 145; //the total amount of pages
$selectedPage = 40; //the selected page

$pages = array(); //the array which is gonna hold the pages we need to display
$offset = 3; //the number of pages to select around the selected page

$closePages = range($selectedPage - $offset, $selectedPage + $offset); //select the pages that are in $offset of the selected page

array_filter($closePages, function($x) { //filter the pages below 1 and above $totalPages
    return ($x <= $totalPages && $x >= 1 ? true : false ); 
});

array_push($pages, 1); //add the first page
array_push($pages, '...'); //add some dots

$pages = array_merge($pages, $closePages);

array_push($pages, '...'); //and again add some dots
array_push($pages, $totalPages); //add the last page

然后使用 foreach 循环显示页面:

foreach($pages as $page) {
    if (is_numeric($page)) {

        if ($page != $selectedPage) $content .= ' <a href="?page=' . $page . '">' . $page . '</a> ';
        else $content .= ' <a href="?page=' . $page . '"><strong>' . $page . '</strong></a> ';

    } else 
        $content .= '[...]';
}

您对此答案发表评论后的一些解释:

$totalPages 变量必须是页面上的页面总数(以您为例),而 $selectedPage 是此时选择的页面。

$totalPages = ceil($pages_query / $limit);
$selectedPage = isset($_GET['page']) ? $_GET['page'] : 1;
于 2013-07-11T13:19:13.153 回答
0

您可以在查询本身中尝试使用 LIMIT

SELECT * FROM TABLE_NAME LIMIT STARTING_RESULT_NUMBER,RESULTS_PER_PAGE

RESULTS_PER_PAGE 是每页要显示的项目数 STARTING_RESULT_NUMBER =(CURRENT_PAGE_NUMBER*RESULTS_PER_PAGE)

于 2013-07-11T13:04:14.523 回答
0

为了将页面列表更改为类似[<] [1] ... [4] [5] [6] ... [45] [>]而不是显示所有页码,您可以将for循环替换为以下内容:

echo "<a class='paginate' href='?page=1'>1</a>";
if($page > 3) {echo "...";}
if($page > 2) {echo "<a class='paginate' href='?page=" . $x-1 . "'>" . $x-1 "</a>";}
if($page != 1 && $page != pages) {echo "<a class='paginate' href='?page=" . $x . "'>" . $x "</a>";}
if($page < $pages-1) {echo "<a class='paginate' href='?page=" . $x+1 . "'>" . $x+1 "</a>";}
if($page < $pages-2) {echo "...";}
if($pages >1) {echo "<a class='paginate' href='?page=1'>1</a>";}

Il 将显示第一页、最后一页、当前页面以及之前和之后的页面。

于 2013-07-11T13:17:32.013 回答