jquery ajax page,jQuery AJAX Pagination

本教程展示了如何利用jQuery AJAX和PHP实现简单的分页功能。通过点击分页链接,AJAX处理器会请求数据库中有限的页面结果。步骤包括:通过AJAX发送分页请求,计算查询限制以获取数据,创建分页链接并应用样式,以及显示结果和分页链接。
摘要由CSDN通过智能技术生成

jQuery AJAX Pagination

by Vincy. Last modified on June 1st, 2021.

In this tutorial, we are going to see the simple code for pagination using jQuery AJAX and PHP. This code will have first, last, previous, next and other pagination links. On clicking each link it invokes AJAX handler to request for limited page results from the database.

The steps for implementing jQuery AJAX pagination are,

Sending page request via AJAX.

Calculate query limit to retrieve data.

Create pagination links and apply styles.

Show results and pagination links.

Sending Page Request via AJAX

In this code, we are sending an initial request for a page getresult.php.

a905fb2784d8bf6476e3cae9787d5671.png

This page will do all server side functions and respond to the pagination request. AJAX handlers insert this response data into the target selectors.

function getresult(url) {    $.ajax({

url: url,

type: "GET",

data: {rowcount:$("#rowcount").val()},

success: function(data){ $("#pagination").html(data); },

error: function() {}

});

}

getresult("getresult.php");

Calculate Query Limit

Since loading bulk of data takes much time, the pagination is for the quick retrieve/load. So, we have to calculate the start and end limit based on the page request. The code is,

require_once("dbcontroller.php");

require_once("pagination.class.php");

$db_handle = new DBController();

$perPage = new PerPage();

$sql = "SELECT * from php_interview_questions";

$paginationlink = "getresult.php?page=";

$page = 1;

if(!empty($_GET["page"])) {

$page = $_GET["page"];

}

$start = ($page-1)*$perPage->perpage;

if($start < 0) $start = 0;

$query = $sql . " limit " . $start . "," . $perPage->perpage;

$faq = $db_handle->runQuery($query);

Create Pagination Links and Apply Styles

For creating a total number of page links, we need to pass the database result count if found already.

if(empty($_GET["rowcount"])) {

$_GET["rowcount"] = $db_handle->numRows($sql);

}

$perpageresult = $perPage->perpage($_GET["rowcount"], $paginationlink);

The function perpage will create all pagination links and apply styles based on the status of the page whether it is active or not applicable. The code is,

function perpage($count,$href) {

$output = '';

if(!isset($_GET["page"]))

$_GET["page"] = 1;

if($this->perpage != 0)

$pages = ceil($count/$this->perpage);

if($pages>1) {

if($_GET["page"] == 1)

$output = $output . '<<<';

else

$output = $output . '<<<';

if(($_GET["page"]-3)>0) {

if($_GET["page"] == 1) $output = $output . '1';

else $output = $output . '1';

}

if(($_GET["page"]-3)>1) {

$output = $output . '...';

}

for($i=($_GET["page"]-2); $i<=($_GET["page"]+2); $i++) {

if($i<1)

continue;

if($i>$pages)

break;

if($_GET["page"] == $i)

$output = $output . ''.$i.''; else $output = $output . ''.$i.'';

}

if(($pages-($_GET["page"]+2))>1) {

$output = $output . '...';

}

if(($pages-($_GET["page"]+2))>0) {

if($_GET["page"] == $pages) $output = $output . '' . ($pages) .'';

else $output = $output . '' . ($pages) .'';

}

if($_GET["page"] < $pages)

$output = $output . '>>>';

else

$output = $output . '>>>';

}

return $output;

}

Show Results and Pagination Links

Finally the requested PHP script will form the output HTML for printing database results and pagination link.

$output = '';

foreach($faq as $k=>$v) {

$output .= '

' . $faq[$k]["question"] . '
';

$output .= '

' . $faq[$k]["answer"] . '
';

}

if(!empty($perpageresult)) {

$output .= '

';

}

print $output;

Data printed above will be read as an AJAX response and inserted into target selector.

Refer AJAX pagination with PHP if you are looking for a PHP solution with jQuery.

success: function(data){

$("#pagination").html(data);

}

Comments to “jQuery AJAX Pagination”

Edwin says:

can you please share the SQL file of this script

Vincy says:

Hi Edwin, it should be available in the download.

Leave a ReplyYour email address will not be published. Required fields are marked *

Comment

Name *

Email *

Leave this empty *

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值