Simple Pagination Code In PHP With Example

simple pagination code in php with demo example

When you are dealing with large number of data, pagination is great option for you. Data pagination is a process in which you will get some amount of data from the database rather than all data. This will increase you application’s speed as well as performance. So in this tutorial we will learn about how to create a simple pagination code with demo example in the PHP. So let’s start step by step.

[sociallocker]
 Download  Demo
[/sociallocker]

I have a one database named “test” with “pagination” table. Pagination table contain some amount of dummy data. I will fetch them from the database and render them on the index.php file using php and mysql. Here is my index.php file. See the code below.

index.php

<!DOCTYPE html>
<html>
<head>
	<title>Simple Pagination Tutorial</title>
	<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css">
	<link rel="stylesheet" type="text/css" href="css/custom.css">

	<!-- Script for Bootstrap JS -->
	<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
	<script type="text/javascript" src="js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="row">
			<div class="col-md-12">
				<center><h2>Pagination Tutorial In PHP</h2></center>
			</div>
		</div>
		<br>
		<div class="row">
			<div class="col-md-12">
				<?php 
				include 'config.php';
				
				$page = 1;
				if(isset($_GET['page']) && !empty($_GET['page']))
				{
					$page = $_GET['page'];
				}

				$sql = "SELECT * FROM Pagination";
				$query = mysqli_query($conn,$sql);
				$row = mysqli_num_rows($query);

				/*Code for pagination*/

				$total_pages = $row/10;
				$total_int_page = (int)$total_pages;
				if($total_int_page < $total_pages){$total_int_page++;}
				$firstpage = 1;
				$lastpage = $total_int_page;

				if($page<=0){$page = 1;}
				if($page>$lastpage){$page = $total_int_page;}

				$limit = 10;
				$offset_page = $page-1;
				if($offset_page != 0) {
					$offset = ($offset_page*$limit)-1;
				} else {
					$offset = 0;
				}	

				$data_sql = "SELECT * FROM Pagination LIMIT $limit offset $offset";
				$query = mysqli_query($conn,$data_sql);

				if($row!=0)
				{
				?>
				<table class="table table-responsive table-bordered">
					<tr>
						<th>Id</th>
						<th>Firstname</th>
						<th>Lastname</th>
					</tr>
					<?php 
					while($data = mysqli_fetch_array($query))
					{
					?>
					<tr>
						<td><?php echo $data['id']; ?></td>
						<td><?php echo $data['f_name']; ?></td>
						<td><?php echo $data['l_name']; ?></td>
					</tr>
					<?php				
					}
					?>
				</table>
				<?php } ?>
				<div class="row">
					<div class="col-md-12">
						<center>
							<ul class="pagination">
								<li><a href="http://127.0.0.1/project/pagination-tutorial/index.php?page=<?php echo $firstpage; ?>"><<</a></li>
								<?php

									if($lastpage<=5)
									{
										$first_count = 1;
										$last_count = $lastpage;
										renderPagination($first_count,$last_count,$page);
									}
									else
									{
										if($page > 2 && $page < $lastpage - 1) {
											$first_count = $page - 2;
											$last_count = $page + 2;
											renderPagination($first_count,$last_count,$page);
										}
										elseif($page<=2)
										{
											$first_count = 1;
											$last_count = 5;
											renderPagination($first_count,$last_count,$page);
										}
										elseif($page>=$lastpage-1 && $page<=$lastpage)
										{
											$first_count = $lastpage-4;
											$last_count = $lastpage;
											renderPagination($first_count,$last_count,$page);
										}
									}
								 ?>
								<li><a href="http://127.0.0.1/project/pagination-tutorial/index.php?page=<?php echo $lastpage; ?>">>></a></li>
							</ul>
						</center>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
</html>

<?php 

function renderPagination($first_count,$last_count,$page)
{
	for ($i=$first_count; $i<=$last_count; $i++) 
	{ 
		$active = "";
		if($i==$page)
		{
			$active = "active";
		}
		echo "<li class='$active'><a href='http://127.0.0.1/project/pagination-tutorial/index.php?page=$i'>$i</a></li>";
	}
}

?>

In the above code i have use some logic for represent the data in the pagination manner. Please check it. If you have any question regarding this tutorial then let me know in the comment box. I hope that you may like this tutorial. Please share this tutorial with your friends. Thanks you for reading this tutrial.

Related:  Complete Registration Form Server Side Validation In PHP

About Chintan Panchal

I am web developer having 6+ year of experience of web development. Currently working on laravel and have worked on codeigniter, cakephp, symfony. Thank you :)

View all posts by Chintan Panchal →

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.