How to create pagination in PHP
File structure for this tutorial :
- config.php (Database configuration file)
- index.php (Main file)
- tbluser.sql (SQL Table structure with dummy data)
Demo———————————————————
First create a table tbluser
. Sturcture of tbluser
:
1 2 3 4 5 6 7 |
CREATE TABLE `tbluser` ( `id` int(11) NOT NULL, `Name` varchar(120) NOT NULL, `PhoneNumber` int(11) NOT NULL, `Emailid` varchar(150) NOT NULL, `PostingDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Insert some data into this table.
We will use LIMIT
clause for generating pagination.LIMIT
clause uses two arguments one is OFFSET and second one is the number of records return from database.
Getting Current page number
We will use $_GET
array for getting current page number.Default page number will be 1.
1 2 3 4 5 |
if (isset($_GET['pageno'])) { $pageno = $_GET['pageno']; } else { $pageno = 1; } |
Formula for pagination
1 2 |
$no_of_records_per_page = 10; $offset = ($pageno-1) * $no_of_records_per_page; |
Getting total number of pages
1 2 3 4 |
$total_pages_sql = "SELECT COUNT(*) FROM tbluser"; $result = mysqli_query($con,$total_pages_sql); $total_rows = mysqli_fetch_array($result)[0]; $total_pages = ceil($total_rows / $no_of_records_per_page); |
SQL Query for Pagination
1 |
$sql = "SELECT * FROM tbluser LIMIT $offset, $no_of_records_per_page"; |
Pagination Buttons
1 2 3 4 5 6 7 8 9 10 11 12 |
<div align="center"> <ul class="pagination" > <li><a href="?pageno=1">First</a></li> <li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>"> <a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a> </li> <li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>"> <a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>">Next</a> </li> <li><a href="?pageno=<?php echo $total_pages; ?>">Last</a></li> </ul> </div> |
Here is the full code that we have written during this tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?php // Database Configuration file include('config.php');?> <html> <head> <title>Pagination</title> <!-- Bootstrap CDN --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <table class="table"> <tr> <th>#</th> <th>Name</th> <th>Phone Number</th> <th>Email</th> <th>Date</th> </tr> <?php //Getting default page number if (isset($_GET['pageno'])) { $pageno = $_GET['pageno']; } else { $pageno = 1; } // Formula for pagination $no_of_records_per_page = 10; $offset = ($pageno-1) * $no_of_records_per_page; // Getting total number of pages $total_pages_sql = "SELECT COUNT(*) FROM tbluser"; $result = mysqli_query($con,$total_pages_sql); $total_rows = mysqli_fetch_array($result)[0]; $total_pages = ceil($total_rows / $no_of_records_per_page); $sql = "SELECT * FROM tbluser LIMIT $offset, $no_of_records_per_page"; $res_data = mysqli_query($con,$sql); $cnt=1; while($row = mysqli_fetch_array($res_data)){?> <tr> <td><?php echo $cnt;?></td> <td><?php echo $row['Name'];?></td> <td><?php echo $row['PhoneNumber'];?></td> <td><?php echo $row['Emailid'];?></td> <td><?php echo $row['PostingDate'];?></td> </tr> <?php $cnt++; } ?> </table> <div align="center"> <ul class="pagination" > <li><a href="?pageno=1">First</a></li> <li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>"> <a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a> </li> <li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>"> <a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>">Next</a> </li> <li><a href="?pageno=<?php echo $total_pages; ?>">Last</a></li> </ul> </div> </body> </html> |
Demo———————————————————
Pagination in PHP(Download full source code)Size: 2.59 KBVersion: V 1.0
How to Create pagination using PHP and MySQLi (With page numbering)