How to create Pagination with PHP and MySql
In this tutorial, I will inform you How to create Pagination with PHP and MySql. If we have more data at that time we need to PHP pagination because if we display all data that will take time. in this example, we will create a simple example of pagination in PHP. so you can see our PHP pagination example.
Pagination means document content dividing into multiple pages. whenever we have one long page at that time we dividing into multiple pages.
if you want to create pagination using MySQL at that time use limit clause, it takes two arguments first is “offset” and the second is the “end”. end means a number of records returned from the database.
I have share post next and previous button with code in this tutorial and I hope it will be helping you. below example in 3 records displayed per page.
PHP pagination Example with Mysql
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | <html> <head> <title>How to create Pagination with PHP and MySql?</title> <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.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script </head> <body> <table border="1" align="center"> <tr> <td colspan="7" align="right"><a href="add.php">Add</a></td> </tr> <tr> <td>First Name</td> <td>Last Name</td> <td>Address</td> <td>Email</td> <td>Mobile</td> <td>Action</td> </tr> <?php include('connection.php'); $total=3; if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; } $start = ($page-1) * $total; $slt="select * from register LIMIT $start,$total"; $rec=mysqli_query($conn,$slt); while($row=mysqli_fetch_array($rec)) { ?> <tr> <td><?php echo $row['first_name']; ?></td> <td><?php echo $row['last_name']; ?></td> <td><?php echo $row['email']; ?></td> <td><?php echo $row['address']; ?></td> <td><?php echo $row['mobile']; ?></td> <td><a href="update.php?edit_id=<?php echo $row['id'];?>">Edit</a> <a href="delete.php?delete_id=<?php echo $row['id'];?>">Delete</a> </td> </tr> <?php } ?> <tr> <td colspan="7"> <ul class="pagination"> <li class="<?php if($page==1){ echo 'disabled';} ?>"> <a href="<?php if($page==1){ echo '#';} else {?><?php echo $_SERVER['PHP_SELF']?>?page=<?php echo $page-1; }?>" aria-label="Previous"> <span aria-hidden="true">Previous</span> </a> </li> <?php $slt="select * from register"; $rec=mysqli_query($conn,$slt); $total1=mysqli_num_rows($rec); $total_pages = ceil($total1 / $total); for($i=1;$i<=$total_pages;$i++) {?> <li class="<?php if($_GET["page"]==$i){ echo 'active'; } ?>"><a href="<?php echo $_SERVER['PHP_SELF']?>?page=<?php echo $i;?>"><?php echo $i;?></a> </li> <?php }?> <li class="<?php if($page==$total_pages){ echo 'disabled'; } ?>"> <a href="<?php if($page==$total_pages){ echo '#';} else {?><?php echo $_SERVER['PHP_SELF']?>?page=<?php echo $page+1; }?>" aria-label="Next"> <span aria-hidden="true">Next</span> </a> </li> </ul> </td> </tr> </table> </body> </html> |