2011-01-09 59 views
0

分頁顯示ASC到DESC是:分頁錯誤

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> 

我告訴現在DESC對於ASC帖子使用這個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; 

什麼php代碼應該改變,因爲顯示DESC到ASC的帖子和分頁顯示下10個帖子

回答

1

更改限制部分(語法LIMIT偏移,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; 
+0

謝謝你,它的作品 – Dan 2011-01-10 07:31:11

1

您可以使用SQL limit命令限制自己10個結果&傳遞的偏移量。 也,你的代碼if($page == 1) { $pstart = 0; } else { $pstart = $page * $totalrows; } $pend = $pstart + $totalrows;
是不正確的 - 對於頁1,$ pstart將爲0,但對於頁2,$ sptart將是20,使您跳過記錄10-19。