Question

I have a PHP pagination that is used when there are more than the desired amount per page on a forum thread. (15) It only lets you go to the previous and next page though. I need to find a way to make a last page link. Here's my code:

    $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.
    }
    }

If anyone could help, I would appreciate it. Thanks.

Was it helpful?

Solution

$lastPage = ceil($getnumbers / 15);

Say you have 115 comments. 115 / 15 = 7.667. So you'll have 7 pages, and a bit left. By using ceil(), you round it to above, so 8. The last page would indeed be 8. 7 pages full of 15 comments, with the last page containing (115 - (7 * 15) =) 10 comments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top