Home » pagination in php using ajax

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:  Web Services Tutorial In PHP For Beginners

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.

Read Also:  How to make PHP Pagination With Example and Demo?

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  jQuery parseInt Convert String to Integer Number
  2. Read Also:  Advanced jQuery Ajax Pagination PHP MySQL using jQuery
  3. Read Also:  How to Open URL in New Tab using Yii Framework
  4. Read Also:  How to Solve Error call to a member function toArray() on null?
  5. Read Also:  Create Thumbnail And Upload image using PHP
  6. Read Also:  Download CentOS 7 – DVD & Minimal ISO Images
  7. Read Also:  Pagination with jQuery Ajax PHP and MySQL
  8. Read Also:  Advanced Pagination with PHP, MySQL and jQuery Demo
  9. Read Also:  PHP Pagination demo – Pagination in PHP with example script
  10. Read Also:  jQuery ajax pagination in php MySQLi

Leave a Comment

Your email address will not be published. Required fields are marked *