How To Create Pagination With PHP And MySql


In this tutorial, I will learn How to create Pagination with PHP and MySql.

Pagination in a documented content dividing into multiple pages of your project. whenever we have one long page at that time we dividing into multiple pages in pagination in PHP.

if you want to create dynamic pagination in PHP with MySQL example, at that time use the limit clause, it takes two arguments first is “offset” and the second is the “end”. the end means a number of records returned from the MySQL database.

I have share post next and previous button with code in this tutorial and I hope it will be helping you. below example simple pagination in PHP with MySQL example.

How To Create [Dynamic Pagination] In PHP With MySQL Example
Example:
<html>
<head>
     <title>How To Create Pagination With PHP And MySql - phpcodingstuff.com </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>&nbsp;&nbsp;<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>
How To Create [Dynamic Pagination] In PHP With MySQL Example

I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close