Skip to Content
PHPCODE
Create Simple Pagination with PHP and MySQL
php code / September 8, 2018

Step by Step
Step 1: Created Database. Database Name demos
Step 2: Created Table SQL QUERY

CREATE TABLE `employee` (
`id` int(11) NOT NULL COMMENT 'primary key',
`employee_name` varchar(255) NOT NULL COMMENT 'employee name',
`employee_salary` double NOT NULL COMMENT 'employee salary'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step 3: Created Insert SQL QUERY

INSERT INTO `employee` (`id`, `employee_name`, `employee_salary`) VALUES
(2, 'Smith', 170750),
(3, 'Jhon', 86000),
(6, 'Andy', 372000),
(7, 'Flower', 137500),
(8, 'Steve', 327900),
(9, 'William', 205500),
(10, 'Dany', 103600),
(11, 'Dove', 120000),
(12, 'Kim', 14000),
(13, 'Frost', 20000);
Step 4: Created header.php File
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<!-- jQuery -->
Step 5: Created footer.php
</div>
</body></html>
Step 6: Created container.php File
</head>
<body class="">
<div role="navigation" class="navbar navbar-default navbar-static-top">
      <div class="container">
        <div class="navbar-header">
          <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a href="https://phpcodeinfomation.blogspot.com/p/php-script.html" class="navbar-brand">PHPCODE</a>
        </div>
        <div class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="https://phpcodeinfomation.blogspot.com/p/php-script.html">Home</a></li>
  </ul>
</div><!--/.nav-collapse -->
      </div>
    </div>
<div class="container" style="min-height:500px;">
<div class=''>
</div>
Step 7: Created db_connect.php file
<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demos";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
if (mysqli_connect_errno()) {
    printf("Connect failed: %sn", mysqli_connect_error());
    exit();
}
?>
Step 8: Created index.php files
/* Your code... */ <?php include('header.php'); include('container.php');?> <div class="container"> <h2>Simple Pagination with PHP and MySQL</h2> <?php include_once("db_connect.php"); $showRecordPerPage = 5; if(isset($_GET['page']) && !empty($_GET['page'])){ $currentPage = $_GET['page']; }else{ $currentPage = 1; } $startFrom = ($currentPage * $showRecordPerPage) - $showRecordPerPage; $totalEmpSQL = "SELECT * FROM employee"; $allEmpResult = mysqli_query($conn, $totalEmpSQL); $totalEmployee = mysqli_num_rows($allEmpResult); $lastPage = ceil($totalEmployee/$showRecordPerPage); $firstPage = 1; $nextPage = $currentPage + 1; $previousPage = $currentPage - 1; $empSQL = "SELECT id,employee_name, employee_salary FROM `employee` LIMIT $startFrom, $showRecordPerPage"; $empResult = mysqli_query($conn, $empSQL); ?> <table class="table "> <thead> <tr> <th>EmpID</th> <th>Name</th> <th>Salary</th> </tr> </thead> <tbody> <?php while($emp = mysqli_fetch_assoc($empResult)){ ?> <tr> <th scope="row"><?php echo $emp['id']; ?></th> <td><?php echo $emp['employee_name']; ?></td> <td><?php echo $emp['employee_salary']; ?></td> </tr> <?php } ?> </tbody> </table> <nav aria-label="Page navigation"> <ul class="pagination"> <?php if($currentPage != $firstPage) { ?> <li class="page-item"> <a class="page-link" href="?page=<?php echo $firstPage ?>" tabindex="-1" aria-label="Previous"> <span aria-hidden="true">First</span> </a> </li> <?php } ?> <?php if($currentPage >= 2) { ?> <li class="page-item"><a class="page-link" href="?page=<?php echo $previousPage ?>"><?php echo $previousPage ?></a></li> <?php } ?> <li class="page-item active"><a class="page-link" href="?page=<?php echo $currentPage ?>"><?php echo $currentPage ?></a></li> <?php if($currentPage != $lastPage) { ?> <li class="page-item"><a class="page-link" href="?page=<?php echo $nextPage ?>"><?php echo $nextPage ?></a></li> <li class="page-item"> <a class="page-link" href="?page=<?php echo $lastPage ?>" aria-label="Next"> <span aria-hidden="true">Last</span> </a> </li> <?php } ?> </ul> </nav> <div style="margin:50px 0px 0px 0px;"> <a class="btn btn-default read-more" style="background:#3399ff;color:white" href="https://phpcodeinfomation.blogspot.com/p/php-script.html">Back</a> </div> </div> <?php include('footer.php');?>
PHPCODE © 2023