2014-02-15 45 views
0

我有一个PHP分页,当在论坛主题上每页所需数量超过所需数量时使用。 (15)它只能让你去上一页和下一页。我需要找到一种方法来制作最后一页的链接。这里是我的代码:PHP - 分页最后一页?

$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

您已经在脚本中拥有所有必要的值 - 因此这只是简单_math_ ... – CBroe

回答

1
$lastPage = ceil($getnumbers/15); 

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

相关问题