How to Pagination in PHP MySQL using Ajax?
1.Create a file and save with Pagination.php:-<<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Pagination in PHP MySQL using Ajax</title>
</head>
<body>
<div class="box-body">
<table class="table table-bordered table-hover" id="examples_1">
</table>
</div>
</body>
</html>
<script>
function loadTable(page)
{
$.ajax({
url: 'ajaxcall.php',
type: 'POST',
data:{page: page},
success: function(data)
{
console.log(data)
$("#examples_1").html(data);
}
});
}
loadTable();
$(document).on("click","#pagination a",function(e){
e.preventDefault();
var page = $(this).attr("id");
alert(page)
loadTable(page);
})
</script>
2.create another file and save with ajaxcall.php:-
1.Connect to database:-
<?php
$limit_per_page=20;
$page = 0;
if(isset($_POST["page"]))
{
$page=$_POST["page"];
}
else
{
$page = 1;
}
$offset_limit = ($page-1) * $limit_per_page;
$sql="SELECT * FROM Employee LIMIT $limit_per_page OFFSET $offset_limit";
$result=mysqli_query($db,$sql);
$output="";
if(mysqli_num_rows($result) > 0)
{
$output.='<table class="table table-bordered table-hover" data-bs-spy="scroll" id="examplep">
<tr>
<td align="center">ID</td>
<td align="center">Name</td>
</tr>
while($row=mysqli_fetch_assoc($result))
{
$output.="<tr>
<td align=''>{$row['id']}</td><td>{$row['name']}</td>
</tr>";
}
$sql_total="SELECT * FROM Employee ";
$records=mysqli_query($db,$sql_total);
$total_record=mysqli_num_rows($records);
$total_pages=ceil($total_record / $limit_per_page);
$output. = '
<ul id="pagination"style="display:flex; text-align:right;">
';
if($total_pages > 4)
{
if($page < 5)
{
for($count = 1; $count <= 5; $count++)
{
$page_array[] = $count;
}
$page_array[] = '...';
$page_array[] = $total_pages;
}
else
{
$end_limit = $total_pages - 20;
if($page > $end_limit)
{
$page_array[] = 1;
$page_array[] = '...';
for($count = $end_limit; $count <= $total_pages; $count++)
{
$page_array[] = $count;
}
}
else
{
$page_array[] = 1;
$page_array[] = '...';
for($count = $page - 1; $count <= $page + 1; $count++)
{
$page_array[] = $count;
}
$page_array[] = '...';
$page_array[] = $total_pages;
}
}
}
else
{
for($count = 1; $count <= $total_pages; $count++)
{
$page_array[] = $count;
}
}
for($count = 0; $count < count($page_array); $count++)
{
if($page == $page_array[$count])
{
$page_link .= '
<li class="page-item active">
<a href="" id='.$page_array[$count].'>'.$page_array[$count].' <span class="sr-only">(current)</span> </a>
</li>
';
$previous_id = $page_array[$count] - 1;
if($previous_id > 0)
{
$previous_link = '<li class="page-item"><a href="javascript:void(0)" id="'.$previous_id.'" >Previous</a><li>';
}
else
{
$previous_link = '
<li class="page-item disabled">
<a href="" >Previous</a>
</li>
';
}
$next_id = $page_array[$count] + 1;
if($next_id >= $total_pages)
{
$next_link = '
<li class="page-item disabled">
<a href="" >Next</a>
</li
';
}
else
{
$next_link = '<li class="page-item"><a href="javascript:void(0)" id="'.$next_id .'">Next</a></li>';
}
}
else
{
if($page_array[$count] == '...')
{
$page_link .= '
<li class="page-item disabled">
<a href="" >...</a>
</li>
';
}
else
{
$page_link .=
' <li class="page-item">
<a href="" id="'.$page_array[$count].'">'.$page_array[$count].'</a>
</li>
';
}
}
}
$output .= $previous_link . $page_link . $next_link;
$output .= '
</ul>
';
echo $output;
}else{
echo "no";
}
?>