我正在建立一个网站,我想添加“上一个”& “下一步”按钮将用户发送到上一页或下一页。所以我试过这个:
<a href="?p=posts&pid='.($_REQUEST['pid']-1).'" class="next_page fa fa-hand-o-left" title="Previous page"></a>
其中'pid'是'post id'我为'next page'按钮做了同样的事情
<a href="?p=posts&pid='.($_REQUEST['pid']+1).'" class="next_page fa fa-hand-o-right" title="Next page"></a>
它有效......但即使具有该ID的页面不存在,它仍会继续。我想知道我是否可以帮助'检查当前页面是第一行还是最后一行。'。
这是我的代码:
<?php
require('connect.php');
if(preg_match('/[^0-9]/i', $_REQUEST['pid'])) {
echo '<div style="margin:50px auto;" class="warning_msg">WARNING: !! Illegal character detected !! >> <b>'.$_REQUEST['pid'].'</b></div>';
} else {
$post_ = $dbh->query('SELECT * FROM `posts` WHERE `id`='.$dbh->quote($_REQUEST['pid']).'')->fetch();
echo '<div class="_header_ p_header_ disable_padding">
<a href="?p=posts&pid='.($_REQUEST['pid']-1).'" class="prev_page fa fa-hand-o-left" title="Previous Post"></a>
'.$post_['title'].'
<a href="?p=posts&pid='.($_REQUEST['pid']+1).'" class="next_page fa fa-hand-o-right" title="Next Post" ></a>
</div>
<div>'.$post_['article'].'</div>
<div class="_footer_">posted by <a href="/profile.php?uid='.$post_['user_id'].'"><i>'.$post_['author'].'</i></a> on » '.date('M d Y H:m:i', strtotime($post_['post_time'])).'</div>';
}
?>
编辑(已解决):
找到第一个&amp;最后一行我使用了一个查询
$page = $dbh->query('SELECT MIN(id), MAX(id) FROM `posts`')->fetch();
并使用如下:
/* if current post id is equals to last id from posts */
if($_REQUEST['pid'] == $page[1]) { $next_page = $_REQUEST['pid'].'#';}
else { $next_page = ($_REQUEST['pid']+1); }
/* if current post id is equals to first id from posts */
if($_REQUEST['pid'] == $page[0]) { $prev_page = $_REQUEST['pid'].'#';}
else { $prev_page = ($_REQUEST['pid']-1); }
<a href="?p=posts&pid='.$prev_page.'" class="prev_page fa fa-hand-o-left" title="Previous Post"></a>
<a href="?p=posts&pid='.$next_page.'" class="next_page fa fa-hand-o-right" title="Next Post"></a>
答案 0 :(得分:0)
<?php
$lastpage = 10;
$ppage = isset($_REQUEST['pid']) && $_REQUEST['pid'] > 1 ? ($_REQUEST['pid']-1) : 0;
$npage = isset($_REQUEST['pid']) && $_REQUEST['pid'] < $lastpage ? ($_REQUEST['pid']+1) : $lastpage;
?>
<a href="?p=posts&pid=<?=$ppage?>" class="next_page fa fa-hand-o-left" title="Previous page"></a>
<a href="?p=posts&pid=<?=$npage?>" class="next_page fa fa-hand-o-right" title="Next page"></a>
这里我为最后一页设置了一个常量值10.而不是你必须找到outlast页面id并替换它.Php分页脚本也可用于查找第一页和最后一页..