PHP分页,具有x个页数
目前我有一个功能正常的分页脚本,尽管我缺少这个特性。目前,在$pages列表中可能会显示数百页,因为在[1][…]5、6、7[…][45]之间没有可显示的过滤器。这是我的密码:PHP分页,具有x个页数,php,pagination,Php,Pagination,目前我有一个功能正常的分页脚本,尽管我缺少这个特性。目前,在$pages列表中可能会显示数百页,因为在[1][…]5、6、7[…][45]之间没有可显示的过滤器。这是我的密码: /** Pagination **/ $limit = 7; $query = "SELECT COUNT(*) FROM users"; $result = $db->prepare($query); $result->execute(); $pages_qu
/** 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();
它被从里面的其他垃圾中剥离出来,但这只是总体框架。基本上,我试图找出如何限制它在页面之间的底部,如问题的顶部所指定的。比如:
[]
如果这有意义的话。您可以在查询本身中尝试限制 从表格中选择*名称限制起始结果编号,每页结果 RESULTS_PER_PAGE是每页要显示的项目数
启动\u RESULT\u NUMBER=当前\u页面\u NUMBER*每页的结果\u为了将页面列表更改为类似[]的内容,而不是显示所有页码,您可以使用以下内容替换for循环:
Il将显示第一页、最后一页、当前页面以及之前和之后的页面。我个人对分页的意思是,它必须可读,并且以后可以简单地更改。 我根据您的示例键入了以下代码:
$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
他已经有了限制,他想像这样显示分页[]我试过这个,但似乎不起作用。它没有显示任何页面之间,只有第一个和最后一个不幸的。我很困惑如何实现这一点。我假设我的$totalPages等于$pages,$selectedPage等于$pages,对吗?我试图实现代码,但页面上什么也没有显示。@JordanLovelle,我在答案下面添加了一些解释。如果你没有得到任何结果,试着填写一些随机数来测试它。
$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($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 = ceil($pages_query / $limit);
$selectedPage = isset($_GET['page']) ? $_GET['page'] : 1;