Pagination table
Real Example in this project you will learn how to make pagination with previous and next button
index.php
<?php include("paging.php"); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1,shrink-to-fit=no">
<title>Codetechinfo </title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<br>
<div class="container w-50" style="margin-top: 30px">
<div class="container">
<table class="table table-bordered table-striped ">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php while($row=mysqli_fetch_array($run)){?>
<tr>
<td><?php echo $row['cid']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<div class="container">
<ul class="pagination float-right" >
<?php
$total_pages = ceil($total_record/$record_per_page);
if($page > 1){
$previous =$page - 1;
echo "<li class='page-item'><a class='page-link bg-primary text-white' href='index.php?page=".$previous."''>Previous</a></li>";
}
for($x = 1; $x <= $total_pages; $x++){
if($x==$page){
echo "<li class='page-item'><a class='page-link bg-secondary text-white'>$x</a></li>";
}else{
echo "<li class='page-item'><a class='page-link' href='index.php?page=".$x."'>$x</a></li>";
}
}
if($total_pages > $page ){
$next = $page + 1;
echo "<li class='page-item'><a class='page-link bg-primary text-white' href='index.php?page=".$next."'>Next</a></li>";
}
?>
</ul>
</div>
</body>
</html>
Database Connection
Database-> dbdemo Table-> customers
CREATE TABLE `customers` (
`cid` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL
);
Create connection with database
myproject/dbcon.php
dbcon.php
$dbhost ="localhost";
$dbuser ="root";
$dbpswd ="";
$dbname ="dbdemo";
$dbcon= mysqli_connect($dbhost,$dbuser,$dbpswd,$dbname);
if($dbcon){
//echo "Connected............!!!";
return $dbcon;
exit();
}else{
die("Connection failed ? ? ?". mysqli_error($dbcon));
}
mysqli_close($dbcon);
Insert some records in database customers table.
Fetch all records and display in table.
Process to make pagination
Process to make pagination with previous and next button.
paging.php
<?php include("dbcon.php"); //This is database connection file
// ===================Pagination Function==============
$qry =" SELECT * FROM customers";
$result= mysqli_query($dbcon,$qry);
$total_record = mysqli_num_rows($result);
// numbering of pagination
$record_per_page = 2;
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
$page = 1;
}
$start_page = ($page -1)*$record_per_page;
// echo $start_page;
$p_query = "SELECT * FROM customers ORDER BY cid DESC LIMIT $start_page, $record_per_page";
$run = mysqli_query($dbcon,$p_query);
css folder
You must download and include bootstrap css source files for good interface OR You can add cdn link from bootstrap website.
myproject/css
bootstrap.min.css