simple pagination concept using php, jquery, mysql | Sanwebcorner

simple pagination concept using php, jquery, mysql

     This is one of the simple pagination concept is used to display the database tables in a proper page view. It is easy to understand by the programmer. we can also set number of data displayed per page , customize this code and use it.

The pagination concept is reduce the work of programmers. In some case the programmers can't create the pages manually for large amount of data in this case the pagination concept is very useful.

The result will be displayed the below diagram.
simple pagination concept

Source Code:

Database :

create database `sanwebcorner`;


Table:

CREATE TABLE IF NOT EXISTS `emp` (
  `EmpID` int(11) NOT NULL AUTO_INCREMENT,
  `EmpName` varchar(150) NOT NULL,
  `Designation` varchar(150) NOT NULL,
  `Age` varchar(5) NOT NULL,
  `Salary` varchar(8) NOT NULL,
  PRIMARY KEY (`EmpID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;



connect.php

<?php
$conn=mysql_connect("localhost","root","") or die("Database could not connect");
mysql_select_db("sanwebcorner",$conn);
?>

index.php:

<?php
include('connect.php');
$perpage=5;
$sql= "select count(*) from emp";
$result=mysql_query($sql);
$count = mysql_fetch_row($result);
$pages=ceil($count[0]/$perpage);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>sanwebcorner | Pagination</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript" src="sdf.js"></script>
<style>
body { margin: 0; padding: 0; font-family:Verdana; font-size:15px }
a
{
text-decoration:none;
color:#FF3300;
}
a:hover
{
color:#000000;
text-decoration:underline;
}
#loading { 
width: 100%; 
position: absolute;
}
#pagination
{
text-align:center;
margin-left:120px;
}
li{
list-style: none; 
float: left; 
margin-right: 16px; 
padding:5px; 
border:solid 1px #dddddd;
color:#0063DC; 
}
li:hover
color:#FF0084; 
cursor: pointer; 
}
</style>
</head>
<body>
<div align="center">
   <div style="margin-top:50px;"><b><a href="sanwebtutorials.blogspot.in" > Pagination concept using php and jquery and mysql </a></b></div>
 <div id="content" ></div>
<table width="800px">
<tr><Td>
<ul id="pagination">
<?php
for($i=1; $i<=$pages; $i++)
{
echo '<li id="'.$i.'">'.$i.'</li>';
}
?>
      </ul>
</td>
</tr></table>
</div> 
</body>
</html>

data.php:

<style>
#tbl
{
width:910px;
margin-top:40px;
border:1px solid #CC6633;
}
#tbl td{
border:1px solid #CC6600;
}
#tbl th
{
border:1px solid #CC6600;
}
</style>
<?php
include('connect.php');
$perpage = 5; 
if($_GET)
{
$page=$_GET['page'];
}
$start = ($page-1)*$perpage;
$sql = "select * from emp order by EmpID limit $start,$perpage";
$rsd = mysql_query($sql);
?>
<table id="tbl">
<th><b>EmpID</b></th>
<th><b>EmpName</b></th>
<th><b>Designation</b></th>
<th><b>Age</b></th>
<th><b>Salary</b></th>
<?php
while($row = mysql_fetch_array($rsd))
{
$eid    = $row['EmpID'];
$ename = $row['EmpName'];
$desig    = $row['Designation'];
$age    = $row['Age'];
$sal    = $row['Salary'];
?>
<tr>
<td><?php echo $eid; ?></td>
<td><?php echo $ename; ?></td>
<td><?php echo $desig; ?></td>
<td><?php echo $age; ?></td>
<td><?php echo $sal; ?></td>
</tr>
<?php
?>
</table>


sdf.js

$(document).ready(function(){
function Display_Load()
{
   $("#loading").fadeIn(100);
}
function Hide_Load()
{
$("#loading").fadeOut('slow');
};
$("#pagination li:first").css({'color' : '#FF0084','border' : 'none'});
$("#content").load("data.php?page=1", Hide_Load());
$("#pagination li").click(function(){
Display_Load();
$("#pagination li")
.css({'border' : 'solid #dddddd 1px'})
.css({'color' : '#0063DC'});
$(this)
.css({'color' : '#FF0084'})
.css({'border' : 'none'});
var pageNum = this.id;
$("#content").load("data.php?page=" + pageNum, Hide_Load());
});
});