if($page == 1) { $pstart = 0; } else { $pstart = $page * $totalrows; }
$pend = $pstart + $totalrows;
和HTML代码从ASC到DESC
<form action="<?=$_SERVER["SCRIPT_NAME"];?>" method="get">
<div>
<a class="page-numbers" href="<?=$_SERVER["SCRIPT_NAME"];?>?p=<?=$page + 1;?>">Next</a>
<input class="page-numbers" name="p" type="text" value="<?=$page + 1;?>" /> <input class="page-numbers" type="submit" value="Jump" />
</div>
</form>
我现在使用此SQL显示DESC到ASC帖子:
$sql = "SELECT posts.Tags as tags, posts.OwnerUserId as postsid, posts.Id as postid, posts.Body as body, posts.Title as title, users.Id as userid, users.DisplayName as usersname FROM posts JOIN users ON posts.OwnerUserId = users.Id WHERE posts.Id >= " . $pstart . " AND posts.Title != '' ORDER by posts.Id DESC LIMIT " . $totalrows;
我应该更改什么PHP代码,因为要显示DESC到ASC帖子和分页以显示接下来的10个帖子
答案 0 :(得分:1)
更改LIMIT部分(语法为LIMIT offset,row_count)
$sql = "SELECT posts.Tags as tags, posts.OwnerUserId as postsid, posts.Id as postid, posts.Body as body, posts.Title as title, users.Id as userid, users.DisplayName as usersname FROM posts JOIN users ON posts.OwnerUserId = users.Id WHERE posts.Id >= " . $pstart . " AND posts.Title != '' ORDER by posts.Id DESC LIMIT " . (($page-1) * $totalrows) .",". $totalrows;
答案 1 :(得分:1)
您可以使用sql LIMIT命令将自己限制为10个结果&amp;传递偏移量。
另外,您的代码if($page == 1) { $pstart = 0; } else { $pstart = $page * $totalrows; }
$pend = $pstart + $totalrows;
不正确 - 对于第1页,$ pstart将为0,但对于第2页,$ sptart将为20,使您跳过记录10-19。