Saturday, 2 November 2019

simple pagination coding in php with mysql


container.php


</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="http://www.phpzag.com" class="navbar-brand">PHPZAG.COM</a>
        </div>
        <div class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="http://www.phpzag.com">Home</a></li>
          
          </ul>
        
        </div><!--/.nav-collapse -->
      </div>
    </div>
   
    <div class="container" style="min-height:500px;">
    <div class=''>
    </div>


db_connect.php


<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phpzag_demos";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
?>





footer.php

</div>
</body></html>



header.php

 





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


index.php

<?php
include('header.php');
?>
<title>phpzag.com : Demo Create Simple Pagination with PHP and MySQL</title>
<?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="http://www.phpzag.com/simple-code-for-pagination-in-php/">Back to Tutorial</a>       
    </div>
</div>
<?php include('footer.php');?>

No comments:

Post a Comment