Written by Sai Sandeep Thota on June 18, 2015 in php > Programming

Simple Pagination using PHP – Tutorial


Get our Latest updates delivered to your mailbox!

I’ve received an email from one of our reader, he has requested for a tutorial on Simple Pagination using PHP. If you have a website with large amount of data, for instance some 100 rows of tabular data. It will be quite painful for the user, if all the 100 rows of data is being displayed in the same page, instead if we show 10 records per page it will enhance user experience.

The procedure we follow here is, first we have declare a variable $reclimit and assigned a value 10 in order to display 10 records. Next we need to calculate the starting record number using $start = $page-1 * $reclimit (We get the current page number from the URL querystring). Total number of records is stored in $total variable which is obtained from num_rows. Using this total variable, we will obtain the total number of pages. Next using a for loop we will display Page Numbers.

simple pagination php

Download Source Code

We’ve used a sample employees table for this tutorial.

Simple Pagination using PHP

	$mysqli = mysqli_connect("localhost","root","password","mydb");
	$reclimit = 3;
		$page = $_GET['page'];
	} else {
		$page = 1;

	$start = (($page-1) * $reclimit);
	$sql = "SELECT * FROM employee";
	$records = $mysqli->query($sql);
	$total = $records->num_rows;
	$tpages = ceil($total / $reclimit);
	$rec = "SELECT * FROM employee ORDER BY first_name ASC LIMIT $start, $reclimit";
	$records = $mysqli->query($rec);
				<table border="0">
							<td><b>First Name</b></td>
							<td><b>Last Name</b></td>
							<td><b>Job Title</b></td>
							while($row = $records->fetch_array()) {
								echo "<tr>";
								echo "<td>".$row['first_name']."</td>";
								echo "<td>".$row['last_name']."</td>";
								echo "<td>".$row['job_title']."</td>";
								echo "<td>".$row['salary']."</td>";
								echo "</tr>";

				<ul class="pagination">
						for($i=1;$i<=$tpages;$i++) {
							echo "<li><a href=index.php?page=".$i.">".$i."</a></li>";

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: