我拥有的只是一个数字。哪个是行数。现在我需要根据该数字创建一个分页UI。这是我的代码:
// for example
$rows_num = 23;
// limit
$per_page = 10;
// the number of pages
$pages_num = ceil($rows_num / $per_page);
if ( $pages_num > 1 ) {
$current_page = isset( $_GET['page'] ) && $_GET['page'] > 0 ? $_GET['page'] : 0;
$back_btn = $current_page != 0 ? "<a href='?page=$current_page-1' >back</a>" : null;
$forward_btn = $pages_num > $current_page ? "<a href='?page=$current_page+1' >forward</a>" : null;
}
我的代码创建了分页框的 back 和 forward 按钮。但我不知道如何创造这样的东西:
// $pages_num = 5
1 2 3 ... 5
// $pages_num = 2
1 2
// $pages_num = 12
1 2 3 ... 12
请参阅?我想制作第一个(如果存在)的3个链接和结束的1个链接(如果存在)。有人怎么办?
答案 0 :(得分:1)
尝试围绕您的前端需求规则进行游戏。我尝试重现您的需求,但可以使用该算法扩展到许多形式的设计:
// for example
$rows_num = 23;
// limit
$per_page = 10;
// the number of pages
$pages_num = ceil($rows_num / $per_page);
if ( $pages_num > 1 ) {
$current_page = isset( $_GET['page'] ) && $_GET['page'] > 0 ? $_GET['page'] : 0;
//your backward
echo ($current_page > 1) ? "<a href='?page=$current_page-1' >back</a>" : null;
for($i = $current_page; $i <= $pages_num; $i++){
echo "<a href='?page=$i' >$i</a>";
if ($i >= 3 && $i % 3) {
echo '... ',$pages_num;
break;
}
}
//your forward
echo ($pages_num > $current_page) ? "<a href='?page=$current_page+1' >forward</a>" : null;
}
答案 1 :(得分:1)
解决方案1: show 3 pages behind and one page ahead
<?php
ini_set('display_errors', 1);
$rows_num = 50;
$per_page = 38;
$pages_num = ceil($rows_num / $per_page);
$current_page = isset( $_GET['page'] ) && $_GET['page'] > 0 ? $_GET['page'] : 1;
$current_page=$current_page>$pages_num ? $pages_num:$current_page;
$counter=0;
$lastLink=($current_page-3) <= 1 ? 1 : $current_page-3;
for($x=$lastLink;$x<=$current_page;$x++)
{
echo $button = "<a href='?page=".($x)."' >".($x)."</a>" ;
echo " ";
$counter++;
if($counter==4)
{
break;
}
}
echo $button = $pages_num > $current_page ? "<a href='?page=".($current_page+1)."' >".($current_page+1)."</a>" : null;
解决方案2:这将添加所有页码链接,以及前进和后退按钮。
<?php
ini_set('display_errors', 1);
$rows_num = 23;
// limit
$per_page = 10;
// the number of pages
$pages_num = ceil($rows_num / $per_page);
if ( $pages_num > 1 )
{
$current_page = isset( $_GET['page'] ) && $_GET['page'] > 0 ? $_GET['page'] : 0;
echo $back_btn = $current_page !=1 ? "<a href='?page=".($current_page-1)."' >back</a> " : null;
for($x=0;$x<$pages_num;$x++)
{
echo $button = "<a href='?page=".($x+1)."' >".($x+1)."</a>" ;
echo " ";
}
echo $forward_btn = $pages_num != $current_page ? "<a href='?page=".($current_page+1)."' >forward</a>" : null;
}