Reputation: 21
the pagination to show ASC to DESC is:
if($page == 1) { $pstart = 0; } else { $pstart = $page * $totalrows; }
$pend = $pstart + $totalrows;
and HTML code is from ASC to 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>
I show now DESC to ASC posts using this SQL:
$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;
What php code should I changes because to show DESC to ASC posts and pagination to show the next 10 posts
Upvotes: 0
Views: 164
Reputation: 9262
You can use the sql LIMIT command to limit yourself to 10 results & pass an offset.
also, your code if($page == 1) { $pstart = 0; } else { $pstart = $page * $totalrows; }
$pend = $pstart + $totalrows;
is incorrect - for page 1, $pstart will be 0, but for page 2, $sptart will be 20, making you skip over records 10-19.
Upvotes: 1
Reputation: 16825
change the LIMIT part (syntax is 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;
Upvotes: 1