PHP-Pagination Example

The below example shows that how to retrieve records from MySQL Database using PHP Pagination technique.

In the below example  included image along with text information. For image and text storing and retrieving you can click here and refer the same.

First of all create a php file called “pagination2.php” with following code in it.

<html>
<head>
<title>Members Information Pagewise</title>
</head>
<body bgcolor=khaki>
<h1 align=center>Members Information Pagewise</h1>
<hr color=green>
<?php
$con = mysqli_connect('localhost','root','','demo');
//per page records
$per_page=1;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}

else {
$page=1;
}

$start_from = ($page-1) * $per_page;

//Selecting the data from table with LIMIT
$query = "SELECT * FROM members2 LIMIT $start_from, 

$per_page";
$result = mysqli_query ($con, $query);
?>

<table align="center" border="1" cellpadding="4" 

cellspacing="4">
<tr bgcolor=white><th>Members 

Name</th><th>Qualification</th><th>Photo</th></tr>

<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr align="center" bgcolor=skyblue>
<td><?php echo $row['memname']; ?></td>
<td><?php echo $row['qual']; ?></td>
<td> <?php echo "<img src='uploaded/".$row['photo']."' 

height=100 width=100 />" ?> 

</tr>

<?php
};
?>
</table>

<div>
<?php

//select all records from members2 table in this example
$query = "select * from members2";
$result = mysqli_query($con, $query);

// Count the total records present in the table
$total_records = mysqli_num_rows($result);

//Function ceil to divide the total records on per page
$total_pages = ceil($total_records / $per_page);

//Lets go to first page , here you can use icons or text to 

give hyperlinks
echo "<center><a href='pagination2.php?page=1'>".'<img 

src=back.ico>'."</a>";

for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination2.php?page=".$i."'>".$i."</a> ";
};
// Lets move to last page
echo "<a 

href='pagination2.php?page=$total_pages'>".'<img 

src=next.ico>'."</a></center>";
?>
<hr color=green>
</div>
</body>
</html>

The output of the above program would produce following image.

 

Share
Share
Scroll to Top