Pagination with previous and next
Project: in this project we will learn how to make pagination previous & next button with number button using function.
To make pagination first we have to insert some data in database that we have done already (click link below).
How to insert data in database Click here
Then we have to fetch all records from database table and display in table.
How to fetch data from database Click here
Pagination table
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>
<div class="container w-50" style="margin-top: 50px">
<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 pagination($page,$limit,$dbcon); ?>
</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
$limit = 3;
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
$page = 1; // if page is less than 1 it will be 1
}
$record_per_page = ($page -1)*$limit;
// echo $record_per_page;
// $total_pages = ceil($total_pages);
$p_query = "SELECT * FROM customers ORDER BY cid DESC LIMIT $record_per_page, $limit";
$run = mysqli_query($dbcon,$p_query);
// ===================Pagination Function==============
function pagination($page,$limit,$dbcon){
$qry =" SELECT * FROM customers";
$result= mysqli_query($dbcon,$qry);
$total_record = mysqli_num_rows($result);
//echo $total_record;
$total_pages = ceil($total_record / $limit);
//for next previous;
if($page > 1){
$previous =$page - 1;
echo "<li class='page-item'><a class='page-link' href='index.php?page=".$previous."'>Previous</a></li>";
}
//numbering of pagination
for($x = 1; $x <= $total_pages; $x++){
if($x==$page){
echo "<li class='page-item'><a class='page-link bg-primary text-white'>$x</a> </li>";
}else{
echo "<li class='page-item'><a class='page-link' href='index.php?page=".$x."'>$x</a></li>";
}
}
//for next button;
if($total_pages > $page ){
$next = $page + 1;
echo "<li class='page-item'> <a class='page-link' href='index.php?page=".$next."'>Next</a></li>";
}
}
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