Simple Pagination Code in php and mysql

Today I am explaining Simple Pagination Code in PHP and MySQL. Pagination in PHP is a very important concept, sometimes we want to display list of records from database on our website but records may be very large and not possible to display it on one page, this type of problem can be solved using pagination, So here i am giving you Pagination Example in PHP with MySQL database, I will explain you pagination through a sample program given below. Lets first discuss about the concept behind the pagination, think what we required we required to decide how many records have to display on one page let say we decide 10 records per page after that we have to calculate how many records are there in the database let say 100 records are in the database then we have to break 100 records in a bunch of 10 records in this way we have 10 pages, now in this example we are displaying 4 links named First, Previous, Next, Last, when we click on Next button it will display next 10 records from the database and if we click on previous button it will display previous 10 records. Let see how we do this with the help of example.


Step1: Create database
            create database test_db;
            Create Table persons;
            create table persons(first_name char(50),last_name char(50),email_address char(100));
            For this example you have to manually insert some records in the table persons


Click Below
Dynamically Add Rows in Select Tag Using PHP


Step2: create a page index.php

<head>
</head>
<body>
<div id="content">
<?php
$con=mysql_connect("localhost","root","root");
mysql_select_db("test_db",$con);

$start=0;
$limit=1;

if(isset($_GET['page_no']))
{
    $page_no=$_GET['page_no'];
    $start=($page_no-1)*$limit;
}
else{
    $page_no=1;
}
$query=mysql_query("select * from persons LIMIT $start, $limit");
echo "<center><table width='600'><tr><td>First Name</td><td>Last Name</td><td>Email</td></tr>";
while($result=mysql_fetch_array($query))
{
    echo "<tr><td>".$result['first_name']."</td><td>".$result['last_name']."</td><td colspan='2'>".$result['email_address']."</td></tr>";
}

$rows=mysql_num_rows(mysql_query("select * from persons"));
$total=ceil($rows/$limit);

echo "<tr><td><a href='?page_no=1' class='button'>First</a></td><td>";
if($page_no>1)
{
    echo "<a href='?page_no=".($page_no-1)."' class='button'>Previous</a>";
}
echo "</td>";
if($page_no!=$total)
{
    echo "<td><a href='?page_no=".($page_no+1)."' class='button'>Next</a></td>";
}

echo "<td><a href='?page_no=".($total)."' class='button'>Last</a></td></tr></table></center>";
?>
</div>
</body>
</html>

Note: If you want to display pages like 1 2 3 .... then you have to add the below highlighted code after $total=ceil($rows/$limit);
echo "<tr><td colspan='4'>";
for($i=0;$i<=$total;$i++){
echo "<a href='?page_no=".($i+1)."'>".($i+1)."</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}
echo "</td></tr>";


Output
 


Note: If you like this post or have any question in your mind then just comment me i will explain you.

Post a Comment

5 Comments

  1. Great work i need this code very much, Thanks

    ReplyDelete
  2. Thanks for posting this code i really need it

    ReplyDelete
  3. can have a page no. like
    1 2 3 4 5 .... 100 |First| |Previous| |Next| |Last|

    hehehe :)

    thanks in advance :D

    ReplyDelete
    Replies
    1. I updated the above code for you see the highlighted code at the end of the post

      Delete