I'm trying to use pagination for a customer table using PHP, MySQL & Bootstrap. I wrote this code:
<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<table class="table mx-auto">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th>Zipcode</th>
<th>City</th>
<th>Company</th>
</tr>
</thead>
<tbody>
<ul class="pagination">
<?php
if(isset($_GET['page'])){
$previous = $_GET['page'] - 1;
if($previous = -1 ){
$previous = 0;
}
if($previous = 0){
echo '<li class="page-item"><a class="page-link" href="#">Previous</a></li>';
}
else{
echo '<li class="page-item"><a class="page-link" href="?page='. $previous.'">Previous</a></li>';
}
}
$page_max = 10;
$entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
$numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
if(isset($_GET['page'])) {
$page = $_GET['page'];
$start = $page * 10;
$end = ($page + 1) * 10;
var_dump($start);
var_dump($end);
}
else{
$page = 0;
$start = $page * 10;
$end = 10;
}
$customers = $database->getUsers("SELECT * FROM customers LIMIT $start, $end");
?>
<li class="page-item"><a class="page-link" href="#">Next</a></li>
</ul>
<?php
foreach($customers as $customer){
$name = $customer ['name'];
$surname = $customer['surname'];
$email = $customer['email'];
$phone = $customer['phone'];
$address = $customer['address'];
$zipcode = $customer['zipcode'];
$city = $customer['city'];
$company = $customer['company'];
$id = $customer['id'];
echo "<tr>
<td>$name</td>
<td>$surname</td>
<td>$email</td>
<td>$phone</td>
<td>$address</td>
<td>$zipcode</td>
<td>$city</td>
<td>$company</td>
</tr>";
}
?>
I want each page to show 10 records from the database so giving a limit to my SQL query. What happends now is the following; page 0 shows 10 records, page 1 shows 20 records, page 2 shows also 20 records but page 9 shows 11 records.
Could anyone help me out fixing this issue?