PHP:试图想出一个;“上一次”;及;“下一步”;链接

PHP:试图想出一个;“上一次”;及;“下一步”;链接,php,math,hyperlink,Php,Math,Hyperlink,我每页显示10条记录。我目前使用的变量是 $total = total number of records $page = whats the current page I'm displaying 我把这个放在我的页面顶部 if ( $_GET['page'] == '' ) { $page = 1; } //if no page is specified set it to `1` else { $page = ($_GET['page']); } // if page is spec

我每页显示10条记录。我目前使用的变量是

$total = total number of records
$page  = whats the current page I'm displaying
我把这个放在我的页面顶部

if ( $_GET['page'] == '' ) { $page = 1; } //if no page is specified set it to `1`
else { $page = ($_GET['page']); }  // if page is specified set it
这里是我的两个链接

    if ( $page != 1 ) { echo '<div style="float:left" ><a href="index.php?page='. ( $page - 1 ) .'" rev="prev" >Prev</a></div>'; }     
    if ( !( ( $total / ( 10 * $page ) ) < $page ) ) { echo '<div style="float:right" ><a href="index.php?page='. ( $page + 1 ) .'" rev="next" >Next</a></div>'; }
如果($page!=1){echo';}
如果(!(($total/(10*$page))<$page)){echo';}
现在我想(除非我没有想到什么),我可以显示“Prev”链接,除了当页面是“1”时。但是,如何才能使“下一步”链接在最后一页上不显示呢?

!(($total/(10*$page))<$page)
!( ( $total / ( 10 * $page ) ) < $page )
我。。。我认为这是不对的

假设
$total
是1000,那么在第99页上,1000/990小于99,但还有另一页要显示


您可能需要检查
10*$page
是否大于或等于
$total

将最后一行代码替换为:

if ($page*10 < $total) echo '<div style="float:right" ><a href="index.php?page='. ( $page + 1 ) .'" rev="next" >Next</a></div>';
if($page*10<$total)回显“”;

如果有余数,则需要将总数除以pagecount
($total/10)
,再加上一页。使用该函数

$page < ceil($total / 10)
$page
分页是web项目中经常出现的问题,通常最好在某个地方创建某种类,或者至少创建一些方便的函数,为您提供所需的一切

下面是一个函数,它将返回可能有用的内容的字典:

function pagination($numberItems, $perPage, $currentPage) {
    $numPages = ceil($numberItems/$perPage);
    return array( 
        'numberPages' => $numPages,
        'start' => ($currentPage - 1) * $perPage + 1,
        'end' => min($currentPage * $perPage, $numberItems),
        'hasNext' => $page != $numPages,
        'hasPrev' => $page != 1
    );
}

您在最后一行有一个错误:

$totalPages = ceil( $total / 10 );
if ( $page < $totalPages ) ) { echo "<div style="float:right" ><a href="index.php?page='. ( $page + 1 ) .'" rev="next" >Next</a></div>"; }
$totalPages=ceil($total/10);
如果($page<$totalPages)){echo”“;}

//让我们在这里获取总页数
$itemsPerPage=10;
$totalItems=105;//举个例子
$pagesNo=ceil($totalItems/$itemsPerPage);
//在此处获取当前页面。。。
$current=(isset($\u GET['page'])?(int)$_GET["page']:1;//如果定义了页面,请使用它,而不是使用1
$current=($current>0)$电流:1;//如果页面为-1,-2。。。例如,使用1
//显示“上一页”按钮
回声($current>1)?"" : "";
//显示当前页码
回声$电流;
//显示“下一步”按钮
回声($current<$pagesNo)?"" : "";

应该是这样的。。。这只是快速代码。。。稍后,处理一些页面范围(设置当前页码之前/之后应显示的页面数…

听起来您需要检测您是在第一页还是最后一页,并针对这些情况“保护”代码

if (/* not the first page */) {
  // print the prev link
}

if (/* not the last page */) {
  // print the next link
}
您已经有了检测page=1的代码,所以现在只需要确定您是否在最后一页(整数除法或ceil()应该是最好的方法)


对于来自SQL的PHP结果页面,有一篇关于PHPFreaks的整洁文章

非常好,谢谢不知道为什么我没有想到它:(我要变得复杂了。再次感谢!是的,分页类是最好的选择。这里有很多这样的类:)
if (/* not the first page */) {
  // print the prev link
}

if (/* not the last page */) {
  // print the next link
}