Pagination problem

loktoy

Established
problima ko po is.. kapaag marami na ako data yun pagination ko ay tuloy tuloy yun number. gusto ko po mangyari is hanggan 1-10 lang ang number ng pagination then masstop na sya dun ang display yun sunod po magging 11 na yun 1st and 12,13 and soon then masstop na sya 20 then babalik ulit sa 1st magiging 21, 22 and soon ito po code ko

if (isset($_GET['page_no']) && $_GET['page_no'] !=="") {
$page_no =$_GET['page_no'];
}else {
$page_no =1;
}

$total_records_per_page = 20;

$offset = ($page_no - 1) * $total_records_per_page;

$previous_page =$page_no -1;

$next_page = $page_no +1;

$result_count = mysqli_query($con,"SELECT COUNT(*) AS total_records FROM registration INNER JOIN lddap ON registration.payee_name = lddap.payee_name WHERE registration.email_address = '$email_address' AND registration.password = '$password'") or die (mysqli_error($con));

$records = mysqli_fetch_array($result_count);

$total_records = $records['total_records'];

$total_no_of_page = ceil($total_records / $total_records_per_page);

// End Pagination php
// Start Query to Database

$slq = "SELECT registration.payee_name, lddap.particular, lddap.amount, lddap.lddap_ada, lddap.date , lddap.status FROM registration INNER JOIN lddap ON registration.payee_name = lddap.payee_name WHERE registration.email_address = '$email_address' AND registration.password = '$password' ORDER BY lddap.date DESC LIMIT $offset , $total_records_per_page" ;


$result = $con->query($slq);
// End Query to database
?>


<!--Start Pagination -->
<div class="pt-1">
<nav aria-label="Page navigation example">
<div class= "container p-10">
<strong>Page <?= $page_no; ?> of <?= $total_records_per_page;?></strong>
<ul class="pagination">
<li class ="page-item"><a class="page-link" <?= ($page_no <= 1) ? 'disabled style="background-color:gray "' : '' ; ?> <?= ($page_no > 1) ? 'href=?page_no=' .$previous_page : '' ; ?>>Previous</a></li>

<?php for ($counter = 1; $counter <= $total_no_of_page; $counter++) { ?>
<?php if ($page_no != $counter){ ?>
<li class ="page-item"><a class="page-link" href="?page_no=<?= $counter; ?>"><?= $counter; ?></a></li><?php } else { ?> <li class ="page-item"><a class="page-link active" ><?= $counter; ?></a></li><?php }?> <?php }?>

<li class ="page-item"><a class="page-link" <?= ($page_no >= $total_no_of_page) ? 'disabled style="background-color:gray"' : '' ; ?> <?= ($page_no < $total_no_of_page) ? 'href=?page_no=' .$next_page : '' ; ?>>Next</a></li>
</div>
</nav>
</div>
<!-- End Pagination -->
 
To achieve the desired pagination with a limit of 10 numbers per page, you can modify your code as follows:

Code:
if (isset($_GET['page_no']) && $_GET['page_no'] !== "") {
    $page_no = $_GET['page_no'];
} else {
    $page_no = 1;
}

$total_records_per_page = 20;
$numbers_per_page = 10;

$offset = ($page_no - 1) * $total_records_per_page;

$previous_page = $page_no - 1;
$next_page = $page_no + 1;

$result_count = mysqli_query($con, "SELECT COUNT(*) AS total_records FROM registration INNER JOIN lddap ON registration.payee_name = lddap.payee_name WHERE registration.email_address = '$email_address' AND registration.password = '$password'") or die(mysqli_error($con));

$records = mysqli_fetch_array($result_count);
$total_records = $records['total_records'];
$total_no_of_page = ceil($total_records / $total_records_per_page);

// Determine the start and end numbers for pagination display
$start_number = ($page_no - 1) * $numbers_per_page + 1;
$end_number = min($start_number + $numbers_per_page - 1, $total_no_of_page);

$slq = "SELECT registration.payee_name, lddap.particular, lddap.amount, lddap.lddap_ada, lddap.date, lddap.status FROM registration INNER JOIN lddap ON registration.payee_name = lddap.payee_name WHERE registration.email_address =  '$email_address' AND registration.password = '$password' ORDER BY lddap.date DESC LIMIT $offset, $total_records_per_page";

$result = $con->query($slq);
?>

<!-- Start Pagination -->
<div class="pt-1">
    <nav aria-label="Page navigation example">
        <div class="container p-10">
            <strong>Page <?= $page_no; ?> of <?= $total_no_of_page; ?></strong>
            <ul class="pagination">
                <li class="page-item">
                    <a class="page-link" <?= ($page_no <= 1) ? 'disabled style="background-color:gray"' : ''; ?> <?= ($page_no > 1) ? 'href=?page_no=' . $previous_page : ''; ?>>Previous</a>
                </li>

                <?php for ($counter = $start_number; $counter <= $end_number; $counter++) { ?>
                    <?php if ($page_no != $counter) { ?>
                        <li class="page-item"><a class="page-link" href="?page_no=<?= $counter; ?>"><?= $counter; ?></a></li>
                    <?php } else { ?>
                        <li class="page-item"><a class="page-link active"><?= $counter; ?></a></li>
                    <?php } ?>
                <?php } ?>

                <li class="page-item">
                    <a class="page-link" <?= ($page_no >= $total_no_of_page) ? 'disabled style="background-color:gray"' : ''; ?> <?= ($page_no < $total_no_of_page) ? 'href=?page_no=' . $next_page : ''; ?>>Next</a>
                </li>
        </div>
    </nav>
</div>
<!-- End Pagination -->

In this code, I've introduced two new variables, $start_number and $end_number, which will determine the range of numbers to display in the pagination. These variables are calculated based on the current page number and the desired number of numbers per page. The $start_number value is calculated as ($page_no - 1) * $numbers_per_page + 1, and the $end_number value is calculated as min($start_number + $numbers_per_page - 1, $total_no_of_page). This ensures that the pagination numbers are within the desired range.

I hope this helps! Let me know if you have any further questions.
 

About this Thread

  • 1
    Replies
  • 293
    Views
  • 1
    Participants
Last reply from:
Unknown user

Online now

Members online
1,007
Guests online
1,095
Total visitors
2,102

Forum statistics

Threads
2,276,161
Posts
28,967,972
Members
1,231,145
Latest member
kennethCASTRO2012
Back
Top