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
$page_max = 10;
$entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
$numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
if(isset($_GET['page']) && $_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>';
}
}
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
$numberOfRecords = 10;
if(isset($_GET['page'])) {
$page = $_GET['page'];
$start = $page * 10;
}
else{
$page = 0;
$start = $page * 10;
}
$customers = $database->getUsers("SELECT * FROM customers LIMIT $start, $numberOfRecords");
?>
<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>";
}
?>
</tbody>
</table>
</div>
It's working except for the next button but I can fix that my own. I would like to know how I can use the pagination below the table. Now it's above the table, copy & pasting the code below the table doesn't work because the table with the customers is build before the variable customers
is defined.
Could anyone help me achieve this?
You can simply put neccessary code for pagination at bottom
<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>
<?php
$page_max = 10;
$entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
$numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
$numberOfRecords = 10;
if(isset($_GET['page'])) {
$page = $_GET['page'];
$start = $page * 10;
}
else{
$page = 0;
$start = $page * 10;
}
$customers = $database->getUsers("SELECT * FROM customers LIMIT $start, $numberOfRecords");
?>
<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>";
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
if(isset($_GET['page']) && $_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>';
}
}
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
?>
</ul>
</div>
Maybe it's time to tryframeworks? Save time and effort
You can define all your variables on top of your HTML file:
<?php
$database = ...;
$page_max = 10;
$entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
$numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
?>
<DOCTYPE html>
...
just store your pagination html in variable and echo at top and bottom when u need like below.
<?php
$paginationhtml = "";
$paginationhtml .= '<ul class="pagination">';
$page_max = 10;
$entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
$numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
if(isset($_GET['page']) && $_GET['page']){
$previous = $_GET['page'] - 1;
if($previous = -1 ){
$previous = 0;
}
if($previous = 0){
$paginationhtml .= '<li class="page-item"><a class="page-link" href="#">Previous</a></li>';
}
else{
$paginationhtml .= '<li class="page-item"><a class="page-link" href="?page='. $previous.'">Previous</a></li>';
}
}
for($i = 0; $i < $numberOfPages; $i++){
$paginationhtml .= '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
$numberOfRecords = 10;
if(isset($_GET['page'])) {
$page = $_GET['page'];
$start = $page * 10;
}else{
$page = 0;
$start = $page * 10;
}
$customers = $database->getUsers("SELECT * FROM customers LIMIT $start, $numberOfRecords");
$paginationhtml .= '<li class="page-item"><a class="page-link" href="#">Next</a></li>';
$paginationhtml .= '</ul>';
?>
// just echo pagination where u need //
<?php echo $paginationhtml; ?>