pagination in php using ajax

Today, We want to share with you pagination in php using ajax.In this post we will show you dynamic pagination in php with mysql example, hear for pagination without refreshing page php we will give you demo and example for implement.In this post, we will learn about jQuery Pagination in PHP Without Page Refresh with an example.

Simple Ajax Pagination With PHP

In this tutorial we learn to all about pagination in php using ajax Examples like as a pagination without refreshing page php, dynamic pagination in php with mysql example, pagination without refreshing page php, Simple jQuery Pagination With PHP MySQLi or many more.

Read Also:  remove all html tags from string php - 4 Ways to Remove HTML Tags From a String?

Create db.php

 $servername = "localhost";
    $username = "root";
    $password = "DF8sdfd5fd";
    $dbname = "pakainfogandhi";

    $conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

Create Landing Page

index.php

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" id="font-awesome-style-css" href="css/bootstrap3.min.css" type="text/css" media="all">

<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
<title>www.pakainfo.com : Source code of simaple pagination</title>
</head>
<body>
<div><h3>Source code : PHP simaple pagination</h1></div>
<div>
<div id="target-content" >loading...</div>

<?php
include('db.php'); 
$limit = 2;
$sql = "SELECT COUNT(id) FROM articles";  
$rs_response = mysqli_query($conn, $sql);  
$row = mysql_fetch_row($rs_response);  
$total_records = $row[0];  
$total_pages = ceil($total_records / $limit); 
?>
<div align="center">
<ul class='pagination text-center' id="pagination">
<?php if(!empty($total_pages)):for($i=1; $i<=$total_pages; $i++):  
			if($i == 1):?>
            <li class='active'  id="<?php echo $i;?>"><a href='pagination.php?page=<?php echo $i;?>'><?php echo $i;?></a></li> 
			<?php else:?>
			<li id="<?php echo $i;?>"><a href='pagination.php?page=<?php echo $i;?>'><?php echo $i;?></a></li>
		<?php endif;?>			
<?php endfor;endif;?>  
</div>
</div>
</body>
<script>
jQuery(document).ready(function() {
jQuery("#target-content").load("pagination.php?page=1");
    jQuery("#pagination li").live('click',function(e){
	e.preventDefault();
		jQuery("#target-content").html('loading...');
		jQuery("#pagination li").removeClass('active');
		jQuery(this).addClass('active');
        var pageNum = this.id;
        jQuery("#target-content").load("pagination.php?page=" + pageNum);
    });
    });
</script>

Created pagination.php

<?php
include('db.php');

$limit = 2;  
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };  
$start_from = ($page-1) * $limit;  
  
$sql = "SELECT * FROM articles ORDER BY title ASC LIMIT $start_from, $limit";  
$rs_response = mysqli_query($conn, $sql);  
?>
<table class="table table-bordered table-striped">  
<thead>  
<tr>  
<th>title</th>  
<th>body</th>  
</tr>  
</thead>  
<tbody>  
<?php  
while ($row = mysql_fetch_assoc($rs_response)) {  
?>  
            <tr>  
            <td><? echo $row["title"]; ?></td>  
            <td><? echo $row["body"]; ?></td>  
            </tr>  
<?php  
};  
?>  
</tbody>  
</table>    

I hope you get an idea about pagination in php using jquery.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.