PHP tutorial - Pagination


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):

<?php
class DataHandler{
private $con;
private $db;

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

$this->con=mysql_connect($DB_HOST,$DB_USER,$DB_PASS);
if (!$this->con) {
  echo "Unable to connect to DB: " . mysql_error();
  exit;
}
$this->db=mysql_select_db($DB_NAME,$this->con);
  if (!$this->db) {
     echo "Unable to select mydbname: " . mysql_error();
  exit;
  }
}


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

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

}

$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";
$result=mysql_query($query);
$str="<table style='width:100%; border-spacing: 0 2px;' cellspacing='0'>";
while($datarow=mysql_fetch_row($result)){
  $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>";
}

  $str.="<tr><td>";

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

$nextpage=$page+1;
$prepage=$page-1;
$str.="<a href='$curPage?page=$prepage' style='text-decoration:none'><<</a>";
$str.="&nbsp;&nbsp;";
$this->showPageNum($numrows,$rlimit,$str,$nextpage-1,$curPage);
$str.="&nbsp;&nbsp;";
$str.="<a href='$curPage?page=$nextpage' style='text-decoration:none'>>></a>";

}

  $str.="</td></tr>";
  $str.="</table>";

}

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

}

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

if($numRows%$rlimit==0)
  $numPages=$numRows/$rlimit; //even num rows
else
  $numPages=$numRows/$rlimit+1; //odd num rows
for($i=1;$i<=$numPages;$i++){
  if($page==$i)
    $str.="$i";
  else
    $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(){
     mysql_close($this->con);
}

}

 

?>



Comments





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.