DEVELOPMENT

Fetching the records from database and displaying that on your website, in such a way that it gets displayed in number of pages with appropriate amount of records on each page is termed as paging. There are number of way to do that.

Here, i'm using Jquery and php to demonstrate how paging is done.

THE SITUATION

pagination


THE DATABASE

A database named 'arc' contains a table 'head', which contains names of 23 persons with their respected surnames.

db snap

INDEX.PHP
<!DOCTYPE html>
<html>
<head><link rel="stylesheet" href=" style.css" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
 $(document).ready(function() {
 
 $("#recdisp").load("table.php?pn=1"); // pn = pagenumber
 
 $(document).on("click", "button", function(){
 var str= $(this).val();
 $("#recdisp").load("table.php?pn="+str);
 
});
 
 });
</script></head>
<body>
<div id="recdisp"></div>
</body>
</html>
    

TABLE.PHP
<?php

$cpn= $_GET['pn']; // current page number

$conn=@mysql_connect("localhost","root","") or die("could not connect to database");
$db=@mysql_select_db("arc",$conn) or die("could not select database");
$query="SELECT * FROM head";
$result=mysql_query($query);
$num=mysql_numrows($result);

$npg=ceil($num/5); // total number of pages

if(!isset($_GET['pn']) || $_GET['pn']<0 || $_GET['pn']>$npg )
{
 header("Location : auth.php"); 
}

$nav='';

for($i = $cpn-2; $i <= $cpn+2; $i++)
{ 
 if($i<=$npg && $i>0)
 {
 if($cpn==$i)
 {
 $nav .= $i;
 }
 
 else{
 
 $nav .= " <button value=\"$i\">$i</button> ";
 }
 }
}


if($cpn!=$npg)
{
 $calc=$cpn+1; // to move one page forward
 $next = "<button value=\"$calc\">next</button> ";
 
}
else 
{
 $next="next";
}
if($cpn!=1)
{
 $calc=$cpn-1; // to move one page backword
 $prev ="<button value=\"$calc\">prev</button> ";;
 
 
}else
{
 $prev="prev"; 
 
}

echo $prev;

echo " ".$nav; 
echo " ".$next;

echo "<table>";

$i = $cpn*5-5 ;
$j=$cpn*5;
if($cpn==$npg)
{ 
 $j=$num; 
 
}


while ($i < $j) {
 
 $roll=mysql_result($result,$i,"roll");
 $name=mysql_result($result,$i,"name");
 
 
 if($i%2==0) // changing alternate background color of rows
 {
 echo "<tr style=\"background-color:#5D7B9D\">";
 }
 else
 {
 echo "<tr style=\"background-color:#ffffff\">";
 }
 
 echo "<td>$roll</td>"."<td>$name</td>";
 echo "</tr>";
 $i++; 

}

echo "</table>";
?>


STYLE.CSS
#recdisp
{
    text-align:center;   
    width : 100%;
}
tr:hover 
{ 
background: #CCF;
cursor: pointer;
}
td,tr
{
    border: 1px solid black;
}
table
{
    border: 1px solid black;
    margin: 0 auto;
    width : 500px;
}    



Leave a Comment  
Download Code(55)





All rights reserved, the content is copyrighted to the author.

RELATED POST