Initial commit
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
$limit = 3;
|
||||
$adjacent = 3;
|
||||
$con = mysqli_connect("192.168.0.27","root","root","db_pagination");
|
||||
if(mysqli_connect_errno()){
|
||||
echo "Database did not connect";
|
||||
exit();
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
include('db.php');
|
||||
|
||||
if(isset($_REQUEST['actionfunction']) && $_REQUEST['actionfunction']!=''){
|
||||
$actionfunction = $_REQUEST['actionfunction'];
|
||||
|
||||
call_user_func($actionfunction,$_REQUEST,$con,$limit,$adjacent);
|
||||
}
|
||||
function showData($data,$con,$limit,$adjacent){
|
||||
$page = $data['page'];
|
||||
if($page==1){
|
||||
$start = 0;
|
||||
}
|
||||
else{
|
||||
$start = ($page-1)*$limit;
|
||||
}
|
||||
$sql = "select * from ajaxpage order by id asc";
|
||||
$rows = $con->query($sql);
|
||||
$rows = $rows->num_rows;
|
||||
|
||||
$sql = "select * from ajaxpage order by id asc limit $start,$limit";
|
||||
|
||||
$data = $con->query($sql);
|
||||
$str='<table><tr class="head"><td>Id</td><td>Firstname</td><td>Lastname</td></tr>';
|
||||
if($data->num_rows>0){
|
||||
while( $row = $data->fetch_array(MYSQLI_ASSOC)){
|
||||
$str.="<tr><td>".$row['id']."</td><td>".$row['firstname']."</td><td>".$row['lastname']."</td></tr>";
|
||||
}
|
||||
}else{
|
||||
$str .= "<td colspan='5'>No Data Available</td>";
|
||||
}
|
||||
$str.='</table>';
|
||||
|
||||
echo $str;
|
||||
pagination($limit,$adjacent,$rows,$page);
|
||||
}
|
||||
function pagination($limit,$adjacents,$rows,$page){
|
||||
$pagination='';
|
||||
if ($page == 0) $page = 1; //if no page var is given, default to 1.
|
||||
$prev = $page - 1; //previous page is page - 1
|
||||
$next = $page + 1; //next page is page + 1
|
||||
$prev_='';
|
||||
$first='';
|
||||
$lastpage = ceil($rows/$limit);
|
||||
$next_='';
|
||||
$last='';
|
||||
if($lastpage > 1)
|
||||
{
|
||||
|
||||
//previous button
|
||||
if ($page > 1)
|
||||
$prev_.= "<a class='page-numbers' href=\"?page=$prev\">previous</a>";
|
||||
else{
|
||||
//$pagination.= "<span class=\"disabled\">previous</span>";
|
||||
}
|
||||
|
||||
//pages
|
||||
if ($lastpage < 5 + ($adjacents * 2)) //not enough pages to bother breaking it up
|
||||
{
|
||||
$first='';
|
||||
for ($counter = 1; $counter <= $lastpage; $counter++)
|
||||
{
|
||||
if ($counter == $page)
|
||||
$pagination.= "<span class=\"current\">$counter</span>";
|
||||
else
|
||||
$pagination.= "<a class='page-numbers' href=\"?page=$counter\">$counter</a>";
|
||||
}
|
||||
$last='';
|
||||
}
|
||||
elseif($lastpage > 3 + ($adjacents * 2)) //enough pages to hide some
|
||||
{
|
||||
//close to beginning; only hide later pages
|
||||
$first='';
|
||||
if($page < 1 + ($adjacents * 2))
|
||||
{
|
||||
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
|
||||
{
|
||||
if ($counter == $page)
|
||||
$pagination.= "<span class=\"current\">$counter</span>";
|
||||
else
|
||||
$pagination.= "<a class='page-numbers' href=\"?page=$counter\">$counter</a>";
|
||||
}
|
||||
$last.= "<a class='page-numbers' href=\"?page=$lastpage\">Last</a>";
|
||||
}
|
||||
|
||||
//in middle; hide some front and some back
|
||||
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
|
||||
{
|
||||
$first.= "<a class='page-numbers' href=\"?page=1\">First</a>";
|
||||
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
|
||||
{
|
||||
if ($counter == $page)
|
||||
$pagination.= "<span class=\"current\">$counter</span>";
|
||||
else
|
||||
$pagination.= "<a class='page-numbers' href=\"?page=$counter\">$counter</a>";
|
||||
}
|
||||
$last.= "<a class='page-numbers' href=\"?page=$lastpage\">Last</a>";
|
||||
}
|
||||
//close to end; only hide early pages
|
||||
else
|
||||
{
|
||||
$first.= "<a class='page-numbers' href=\"?page=1\">First</a>";
|
||||
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
|
||||
{
|
||||
if ($counter == $page)
|
||||
$pagination.= "<span class=\"current\">$counter</span>";
|
||||
else
|
||||
$pagination.= "<a class='page-numbers' href=\"?page=$counter\">$counter</a>";
|
||||
}
|
||||
$last='';
|
||||
}
|
||||
|
||||
}
|
||||
if ($page < $counter - 1)
|
||||
$next_.= "<a class='page-numbers' href=\"?page=$next\">next</a>";
|
||||
else{
|
||||
//$pagination.= "<span class=\"disabled\">next</span>";
|
||||
}
|
||||
$pagination = "<div class=\"pagination\">".$first.$prev_.$pagination.$next_.$last;
|
||||
//next button
|
||||
|
||||
$pagination.= "</div>\n";
|
||||
}
|
||||
|
||||
echo $pagination;
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<title>Ajax table pagination with MySQL, PHP and jQuery - InfoTuts</title>
|
||||
<head>
|
||||
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="style.css" />
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div id="mhead"><h2>Ajax table pagination with MySQL, PHP and jQuery - InfoTuts</h2></div>
|
||||
<div id="pagination" cellspacing="0">
|
||||
<?php include('dbmanupulate.php');?>
|
||||
</div>
|
||||
|
||||
<script type="text/javascript" src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,36 @@
|
||||
|
||||
$(function(){
|
||||
$.ajax({
|
||||
url:"dbmanupulate.php",
|
||||
type:"POST",
|
||||
data:"actionfunction=showData&page=1",
|
||||
cache: false,
|
||||
success: function(response){
|
||||
|
||||
$('#pagination').html(response);
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
$('#pagination').on('click','.page-numbers',function(){
|
||||
$page = $(this).attr('href');
|
||||
$pageind = $page.indexOf('page=');
|
||||
$page = $page.substring(($pageind+5));
|
||||
|
||||
$.ajax({
|
||||
url:"dbmanupulate.php",
|
||||
type:"POST",
|
||||
data:"actionfunction=showData&page="+$page,
|
||||
cache: false,
|
||||
success: function(response){
|
||||
|
||||
$('#pagination').html(response);
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
return false;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
|
||||
--
|
||||
-- Table structure for table `ajaxpage`
|
||||
--
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `ajaxpage` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`firstname` varchar(30) NOT NULL,
|
||||
`lastname` varchar(30) NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
|
||||
|
||||
--
|
||||
-- Dumping data for table `ajaxpage`
|
||||
--
|
||||
|
||||
INSERT INTO `ajaxpage` (`id`, `firstname`, `lastname`) VALUES
|
||||
(1, 'sanjeev', 'shrivastava'),
|
||||
(2, 'demo', 'demo'),
|
||||
(3, 'jayant', 'jain'),
|
||||
(4, 'sattu ', 'don'),
|
||||
(5, 'ddd', 'ddddd'),
|
||||
(6, 'fffff', 'ffffffff'),
|
||||
(7, 'fffff', 'fffffffff'),
|
||||
(8, 'ffffffffff', 'fffffffffffffff'),
|
||||
(9, 'gfd', 'dfgdfgdfg'),
|
||||
(10, 'gfhgdfg', 'dfgdfgdfgdfg'),
|
||||
(11, 'dffds', 'fdssssssssssssssss'),
|
||||
(12, 'sfdddddddddddd', 'dfssssssssssssssss'),
|
||||
(13, 'fffff', 'fffffffff'),
|
||||
(14, 'ffffffffff', 'fffffffffffffff'),
|
||||
(15, 'gfd', 'dfgdfgdfg'),
|
||||
(16, 'gfhgdfg', 'dfgdfgdfgdfg'),
|
||||
(17, 'dffds', 'fdssssssssssssssss'),
|
||||
(18, 'sfdddddddddddd', 'dfssssssssssssssss');
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Table structure for table `ajaxtable`
|
||||
--
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `ajaxtable` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`firstname` varchar(30) NOT NULL,
|
||||
`lastname` varchar(30) NOT NULL,
|
||||
`domain` varchar(30) NOT NULL,
|
||||
`email` varchar(50) NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
|
||||
|
||||
--
|
||||
-- Dumping data for table `ajaxtable`
|
||||
--
|
||||
|
||||
INSERT INTO `ajaxtable` (`id`, `firstname`, `lastname`, `domain`, `email`) VALUES
|
||||
(1, 'gbvb', 'hbjhbj', 'bnmnmnb', 'bnnn@fdf.vhbh'),
|
||||
(2, 'hjghgh', 'vg', 'hjggj', 'sasa@sasa.vim');
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Table structure for table `demoajax`
|
||||
--
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `demoajax` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`firstname` varchar(30) NOT NULL,
|
||||
`lastname` varchar(30) NOT NULL,
|
||||
`domain` varchar(30) NOT NULL,
|
||||
`email` varchar(50) NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
|
||||
|
||||
--
|
||||
-- Dumping data for table `demoajax`
|
||||
--
|
||||
|
||||
INSERT INTO `demoajax` (`id`, `firstname`, `lastname`, `domain`, `email`) VALUES
|
||||
(1, 'sanjeev', 'shrivastava', 'infotuts', 'sanjsseev00733@gmail.com'),
|
||||
(2, 'InfoTuts', 'InfoTuts', 'InfoTuts.com', 'admin@infotuts.com');
|
||||
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||
@@ -0,0 +1,55 @@
|
||||
#mhead{
|
||||
background-color: #f5f5f5;
|
||||
border: 1px solid #e3e3e3;
|
||||
-webkit-border-radius: 4px;
|
||||
-moz-border-radius: 4px;
|
||||
border-radius: 4px;
|
||||
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
|
||||
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
|
||||
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
|
||||
text-align: center;
|
||||
font-family: georgia;
|
||||
}
|
||||
#pagination table{
|
||||
width:960px;
|
||||
margin:0px auto;
|
||||
font-family:Tahoma,Arial,Verdana,sans-serif;
|
||||
font-size:13px;
|
||||
padding:4px;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
.head{
|
||||
background:lightseagreen;
|
||||
}
|
||||
#pagination table tr td{
|
||||
padding: 8px;
|
||||
text-transform:capitalize;
|
||||
border:1px solid #d1d1d1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
.pagination{
|
||||
width:600px;
|
||||
margin:0px auto;
|
||||
}
|
||||
.pagination .current{
|
||||
padding: 4px 10px;
|
||||
color: black;
|
||||
margin: 1px 0px 9px 6px;
|
||||
display: block;
|
||||
text-decoration:none;
|
||||
float: left;
|
||||
text-transform: capitalize;
|
||||
background: whitesmoke;
|
||||
}
|
||||
.pagination .page-numbers{
|
||||
padding: 4px 10px;
|
||||
color: white !important;
|
||||
margin: 1px 0px 9px 6px;
|
||||
display: block;
|
||||
text-decoration:none;
|
||||
float: left;
|
||||
text-transform: capitalize;
|
||||
background: #00b4cc;
|
||||
}
|
||||
Reference in New Issue
Block a user