我正在使用分页脚本来控制页面上显示消息系统每页显示的记录数量。
我使用的代码如下:
//PAGINATION//
global $pagination;
$total = $this->countInbox();
$adjacents = 3;
$targetpage = "messages.php?inbox"; //your file name
$limit = 10; //how many items to show per page
if(isset($_GET['page']))
{
$page = $_GET['page'];
}else{
$page = 0;
}
if($page){
$start = ($page - 1) * $limit; //first item to display on this page
}else{
$start = 0;
}
/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is current page - 1
$next = $page + 1; //next page is current page + 1
$lastpage = ceil($total/$limit); //lastpage.
$lpm1 = $lastpage - 1; //last page minus 1
/* CREATE THE PAGINATION */
$counter = "";
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<ul class='pagination'>";
if ($page > (int)$counter+1) {
$pagination.= "<li><a href=\"$targetpage&page=$prev\"><</a></li>";
}
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li class='active'><a href='#' class='active'>$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage&page=$counter\">$counter</a></li>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<li class='active'><a href='#' class='active'>$counter</a></li>";
else
$pagination.= "<li ><a href=\"$targetpage&page=$counter\">$counter</a></li>";
}
$pagination.= "<li><a href=\"$targetpage&page=$lpm1\">$lpm1</a></li>";
$pagination.= "<li><a href=\"$targetpage&page=$lastpage\">$lastpage</a></li>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<li><a href=\"$targetpage&page=1\">1</a></li>";
$pagination.= "<li><a href=\"$targetpage&page=2\">2</a></li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<li class='active'><a href='#' class='active'>$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage&page=$counter\">$counter</a></li>";
}
$pagination.= "<li><a href=\"$targetpage&page=$lpm1\">$lpm1</a></li>";
$pagination.= "<li><a href=\"$targetpage&page=$lastpage\">$lastpage</a></li>";
}
//close to end; only hide early pages
else
{
$pagination.= "<li><a href=\"$targetpage&page=1\">1</a></li>";
$pagination.= "<li><a href=\"$targetpage&page=2\">2</a></li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage;
$counter++)
{
if ($counter == $page)
$pagination.= "<li class='active'><a href='#' class='active'>$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage&page=$counter\">$counter</a></li>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<li><a href=\"$targetpage&page=$next\">></a></li>";
else
$pagination.= "";
$pagination.= "</ul>\n";
}
除了一件事之外,它的效果非常好。如果我手动将页面输入到不存在的URL中,则会显示没有记录的页面,例如
如何停止手动输入,以便在页面= 2或页面= 3不存在时,它会自动将用户重定向到默认页面,例如messages.php?收件箱。
我已经在使用其他功能来控制其他有效页面了。
$validPages = array('inbox', 'archive','compose', 'mid', 'trash', 'deleted');
$pageFound = false;
foreach ($validPages as $validPage) {
if (isset($_GET[$validPage])) {
$pageFound = true;
break;
}
}
if (! $pageFound) {
header("Location: messages.php?inbox");
exit;
}
我想知道是否有办法在上面的函数中加入修复。
提前谢谢。
答案 0 :(得分:2)
如何停止手动输入
你不能。
您无法控制访问者的要求。
因此,如果页面= 2,或者页面= 3不存在,那么它会自动将用户重定向到默认页面,例如messages.php?inbox
您知道有多少页($lastpage
)以及要求的页面($page
)。
只需比较这两个数字。
if ($page > $lastpage) { ... }
然后,您可以输出404 Not Found
(或重定向,但404会更有意义)。