PHPWeb Development

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———————————————————

/p>
First create a table tbluser. Sturcture of tbluser :

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.

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 Query for Pagination

$sql = "SELECT * FROM tbluser LIMIT $offset, $no_of_records_per_page";

Pagination Buttons

<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:

<?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 KB
Version: V 1.0

Recommended Tutorials for you


Tags

Anuj kumar

This is Anuj Kumar. I’m a professional web developer with 5+ years of experience. I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials. .

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Check Also
Close
Back to top button
Close
Close