PHP - 分页最后一页?

时间:2014-02-15 22:43:38

标签: php pagination forum

我有一个PHP分页,当论坛帖子上每页的数量超过所需数量时使用。 (15)它只允许你去上一页和下一页。我需要找到一种方法来建立最后一页链接。这是我的代码:

    $resulte = mysqli_query($conexion, "SELECT * FROM forumcomments WHERE forum='$id'");
    $getnumbers = mysqli_num_rows($resulte);
    $howmuchpost = $page * 15;
    $pageplus = $page + 1;
    $pageminus = $page - 1;
    $howmuchpostminus = $howmuchpost - 15;
    $postsleft = $getnumbers - $howmuchpostminus;
    if ($postsleft >= 15) {$nextpage = 'true';}
    if ($howmuchpostminus >= 15 && $page != 1) {$previouspage = 'true';}
    $posts_sql = "SELECT * FROM forumcomments WHERE forum = $id LIMIT ".$howmuchpostminus.", 15";
    $posts_result = mysqli_query($conexion, $posts_sql);

    //Display posts
    $result2 = mysqli_query($conexion, "SELECT * FROM forumcomments WHERE forum='$id' ORDER BY id LIMIT $howmuchpostminus, $howmuchpost") or die(mysqli_error($conexion));
    $num = mysqli_num_rows($result2); 
    if(!$posts_result)
    {
    echo '<tr><td>The posts could not be displayed, please try again later.</tr></td></table>';
    }else{
    while($rows = mysqli_fetch_assoc($posts_result)){
    // Now you obviously have the looping data here, removed because I don't think  this is really needed.
    }
    }

如果有人可以提供帮助,我将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

$lastPage = ceil($getnumbers / 15);

假设您有115条评论。 115/15 = 7.667。所以你将有7页,还有一点。通过使用ceil(),你将它四舍五入到上面,所以8.最后一页确实是8. 7页满15条评论,最后一页包含(115 - (7 * 15)=)10条评论。