Linnea Anderson
Linnea Anderson

Reputation: 147

Pagination will not go beyond the post

Next and Previous pagination. For previous pagination page not go to negative like not below to 0 but for Next it should not go more than the posts. When i click next it will make the pages to the amount of click going more than the posts. For precious i have done this but what do i do for next. How to disable the next when the posts end or page will not go more then the posts. I can't figure this out. Any help would be appreciate.

php

<?php 
$page = (isset($_GET['page']) && $_GET['page'] > 0) ? (int)$_GET['page'] : 1;
$perpage = 10;
$limitp = ($page > 1) ? ($page * $perpage) - $perpage : 0;


$query = mysqli_query($dbc, "SELECT SQL_CALC_FOUND_ROWS * FROM test LIMIT {$limitp}, {$perpage}");
$records = mysqli_fetch_all($query);

$total = mysqli_query($dbc, "SELECT FOUND_ROWS() as total");
$total = mysqli_fetch_assoc($total)['total'];

$pages = ceil($total/$perpage);

?>

<?php 
include ("connection.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Pagination</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

<div>
    <a href="?page=<?php $pagep = $page -1; echo $pagep; ?>">Prev</a>
    <a href="?page=<?php $pagen = $page +1; echo $pagen; ?>">Next</a>
</div>

<table>
    <tr>
        <th>ID</th>
        <th>Email</th>
    </tr>
    <?php foreach ($records as $record): ?>
        <tr>
            <td><?php echo $record[0]; ?></td>
            <td><?php echo $record[4]; ?></td>
        </tr>
    <?php endforeach; ?>
</table>
</body>
</html>

Upvotes: 2

Views: 94

Answers (1)

Sachin Sarola
Sachin Sarola

Reputation: 1041

you need to add condition for prev button if current page is greter than 1 than and than you display prev btn same as for next button if you current page is less total pages (i.e totalrecord / limit in your case limit is 10 and if total record is 22 than 22/10 is equal to 2.2 get ceil valu which make 2.2 to 3) so if current page is less than 3 than and than display next buttton

hope its make sense I am also trying to solved in your code hope this code work

<?php 
$page = (isset($_GET['page']) && $_GET['page'] > 0) ? (int)$_GET['page'] : 1;
$perpage = 10;
$limitp = ($page > 1) ? ($page * $perpage) - $perpage : 0;
$limitn = ($limito)

$query = mysqli_query($dbc, "SELECT SQL_CALC_FOUND_ROWS * FROM test LIMIT {$limitp}, {$perpage}");
$records = mysqli_fetch_all($query);

$total = mysqli_query($dbc, "SELECT FOUND_ROWS() as total");
$total = mysqli_fetch_assoc($total)['total'];

$pages = ceil($total/$perpage);

?>

<?php 
include ("connection.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Pagination</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

<div>
<?php 
if($page>1){
?>
    <a href="?page=<?php $pagep = $page -1; echo $pagep; ?>">Prev</a>
<?php
}
?>

<?php 
if($page<$pages){
?>
    <a href="?page=<?php $pagen = $page +1; echo $pagen; ?>">Next</a>
<?php
}
?>
    
</div>

<table>
    <tr>
        <th>ID</th>
        <th>Email</th>
    </tr>
    <?php foreach ($records as $record): ?>
        <tr>
            <td><?php echo $record[0]; ?></td>
            <td><?php echo $record[4]; ?></td>
        </tr>
    <?php endforeach; ?>
</table>
</body>
</html>

Upvotes: 3

Related Questions