PHP MySQL Pagination using Bootstrap4
How to implement PHP MySQL Pagination using Bootstrap4
Pagination is actually the process of printing database table records in a page wise. Here is the example of pagination code using PHP and Bootstrap-4 with Next and Previous buttons.
Create the following Database and table in MySQL in order to run the below given code.
Create Database
Create pagination.php in server of your choice like XMPP or WAMP and copy and paste the code and run the file.
pagination.php
TOP
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Datatable with mysql</title> <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css"/> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <div class="container"> <div class=""> <?php if (isset($_GET['page_no']) && $_GET['page_no']!="") { $page_no = $_GET['page_no']; } else { $page_no = 1; } // Enter your Host, username, password, database below. $con = mysqli_connect("localhost","root","","usersdb"); if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); die(); } $total_records_per_page = 3; $offset = ($page_no-1) * $total_records_per_page; $previous_page = $page_no - 1; $next_page = $page_no + 1; $adjacents = "2"; $pagLink = ""; $result_count = mysqli_query( $con, "SELECT COUNT(*) As total_records FROM `users`" ); $total_records = mysqli_fetch_array($result_count); $total_records = $total_records['total_records']; $total_no_of_pages = ceil($total_records / $total_records_per_page); $second_last = $total_no_of_pages - 1; // total pages minus 1 $result = mysqli_query( $con, "SELECT * FROM `users` LIMIT $offset, $total_records_per_page" ); ?> <table class="table table-striped table-bordered"> <thead> <tr> <th style='width:50px;'>ID</th> <th style='width:150px;'>Name</th> <th style='width:50px;'>Age</th> <th style='width:150px;'>Email</th> </tr> </thead> <tbody> <?php while($row = mysqli_fetch_array($result)){ echo "<tr> <td>".$row['id']."</td> <td>".$row['username']."</td> <td>".$row['age']."</td> <td>".$row['email']."</td> </tr>"; } mysqli_close($con); ?> </tbody> </table> <nav aria-label="Page navigation example"> <div class="clearfix"></div> <ul class="pagination"> <?php if($page_no>=2) { echo '<li class="page-item"><a class="page-link" href="pagination.php?page_no='. ($page_no-1).'"'.'> Prev </a></li>'; } for ($i=1; $i<=3; $i++) { if ($i == $page_no) { $pagLink .= '<li class="page-item active"><a class="page-link" href="pagination.php? page_no='.$i.'">'.$i. '</a></li>'; } else { $pagLink .= '<li class="page-item"><a class="page-link" href="pagination.php?page_no='.$i.'">'.$i.'</a></li>'; } }; echo $pagLink; if($page_no<$total_no_of_pages){ echo '<li class="page-item"><a class="page-link" href="pagination.php?page_no='. ($page_no+1).'"'.'> Next </a></li>'; } ?> </ul> </nav> </body> </html>
Result:
Comments
Post a Comment