PHP tutorial - Pagination


In the previous page, you learnt to create a comment form to allow visitors to post comments and display the comments on a page which the comments were posted. If there are a lot of comments on the page, it is too large to display all at once. So it is a good idea to split the comments in to different pages. To implement pagination to the comments, you will need to modify the datamip.php file in the previous tutorial to the one shown below.

After you make change to the datamanip.php and run the index.php page again, you see the result as shown below. You can click the << and >> symbols to move one page forward and backward. Alternatively, you can click 1, 2, or 3... to go to page 1, 2, or 3...

paging or pagination in php


datamip.php file (modified to implement pagination):

class DataHandler{
private $con;
private $db;

public function DataHandler($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME){

if (!$this->con) {
  echo "Unable to connect to DB: " . mysql_error();
  if (!$this->db) {
     echo "Unable to select mydbname: " . mysql_error();

public function getComments($tblname,$curPage,$rlimit){

$queryc="SELECT * FROM ".$tblname." WHERE url='$curPage' ORDER BY pdate DESC";
  if($numrows>0) {
     if(!isset($_REQUEST['page'])){ //page displayed at the first time
else{ //now it is not on the first page


$left_rec=$numrows-$start; //Number of records not shown until now
//select $rlimit records from the the table
$query="SELECT * FROM ".$tblname." ORDER BY pdate DESC LIMIT $start, $rlimit";
$str="<table style='width:100%; border-spacing: 0 2px;' cellspacing='0'>";
  $str.="<tr style='background-color:#f8f8f8'><td style='vertical-align: top; width: 78%'><img src='noname.png' width='50' height='50' style='float: left'/><p style='color:#ff9933;'>"."&nbsp;".$datarow[1]."</p><p style='padding-left:15px;word-wrap: break-word;'>".$datarow[2]."</p></td>"."<td style='text-align:right; width: 20%; vertical-align: top; border: 0px solid #f8f8f8'>".$datarow[3]."</td></tr>";


  if($left_rec<$rlimit){//no record to show
    //show previous page
    $str.="<a href='$curPage?page=$page' style='text-decoration:none'><<</a>";
    //show page num
  else if($page==1 && $left_rec!=$rlimit){ //It is on the first page and there are left records to show
    //show page num
      //show next page
    $str.="<a href='$curPage?page=$page' style='text-decoration:none'>>></a>";
  else if($page>1){//It is not on the first page and there are left records to show

$str.="<a href='$curPage?page=$prepage' style='text-decoration:none'><<</a>";
$str.="<a href='$curPage?page=$nextpage' style='text-decoration:none'>>></a>";




  $str.="<p><strong>No comments</strong></p>";
  return $str;


function showPageNum($numRows,$rlimit,&$str,$page,$url){

  $numPages=$numRows/$rlimit; //even num rows
  $numPages=$numRows/$rlimit+1; //odd num rows
    $str.="<a href='$url?page=$i' style='margin-left:5px;margin-right: 5px;'>$i</a>";



public function addComment($name,$comment,$cdate,$url){

$query = "INSERT INTO tblcomments(pname,comment,pdate,url) Values('$name','$comment','$cdate','$url')";
$result = mysql_query($query) or die ("Error:Couldn't execute query. Sorry for the inconvenience!\nPlease try again.");
echo "<p><h4 style='color:#0000FF'>Thank you! Your comment has been published.</h4></p>";


public function closeConnection(){





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.