php ajax 分页phpapi,适用于PHP的AJAX分页解决方案

博客内容讨论了在切换到Ajax后遇到的分页问题。作者提到,由于Ajax加载内容时不改变URL,传统的分页系统无法工作。提出了使用jQuery的load()函数结合页面链接点击事件来实现Ajax分页,并提供了示例代码。同时,讨论了如何处理URL变化以提供更好的用户体验,包括使用HTML5的history.pushState或使用hash值。
摘要由CSDN通过智能技术生成

现在,我使用的分页系统需要网址

因此它使用$_GET方法来找出用户所在的页面.

我决定将我的大部分网站切换到Ajax,然后遇到了问题.当我使用Ajax在页面上加载新内容时,URL始终保持不变,例如http://mypage.com/index.php.因此,我使用的分页系统没有用.

我找不到高效的AJAX分页系统(例如,有些地方比较落后,每当用户单击下一页时,最需要用户将其滚动到该提示,因为当他们单击下一页时,它们始终位于页面底部等)

因此,我决定向您询问是否有人拥有适用于Ajax的有效分页解决方案.

需要分页的示例:

$sql = mysql_query("SELECT * FROM myMembers WHERE username='$username' LIMIT 1") or die (mysql_error("There was an error in connection"));

//Gather profile information

while($row = mysql_fetch_assoc($sql)){

$username = $row["username"];

$id = $row["id"];

$data_display .= ''.Name.' has an id of '.$id.'';

}

<?php echo "$data_display"; ?> //and I need to paginate this entries

jQuery,它将来自不同页面的新内容加载到#content div中

function viewHome(){

$('#woodheader').load("inc/home_top.php", function () {

$(this).hide().fadeIn(700)

});

$('#content').html('Loading..   ajax_loader.gif').load("inc/home.php", function () {

$(this).hide().fadeIn(700)

});

}

function viewAbout(){

$('#woodheader').load("inc/about_top.php", function () {

$(this).hide().fadeIn(700)

});

$('#content').html('Loading..   ajax_loader.gif').load("inc/about.php", function () {

$(this).hide().fadeIn(700)

});

}

function viewProducts(){

$('#woodheader').load("inc/products_top.php", function () {

$(this).hide().fadeIn(700)

});

$('#content').html('Loading..   ajax_loader.gif').load("inc/products.php", function () {

$(this).hide().fadeIn(700)

});

}

解决方法:

分页并不像您想象的那么难,可以使用jQuery的load()函数将内容加载到具有页面内容的元素中.

例如,您有:

Page 1

Page 1

Page 3

$.ready(function(){

var currPage = =$pageNumber; ?>; // The page number loaded on page refresh

$('#link1,#link2,#link3').onclick(function(){

// Get the first number inside the id

var pageNum = parseInt($(this).attr('id'));

// Don't load the same page

if(currPage == pageNum) return;

// Show loading animation or whatever

// Load the page using ajax

$('#page-content').load('pages.php?page='+pageNum, function(){

// End loading animation

currPage = pageNum;

});

return false; // Important for not scrolling up

});

});

关于URL,当用户单击页面链接时,有三个选项可供选择:

>只需加载页面即可更改网址

>使用HTML5 history.pushState(请参阅MDN resource)(如果受支持),并将选项3作为不支持的浏览器的后备

>使用#page1,#page1等作为链接的href值,以便用户知道他们在哪个页面上并解析php中url的值:

$uri = explode('#page', $_SERVER['REQUEST_URI']);

$pageNumber = intval($uri[1]);

标签:ajax,pagination,javascript,mysql,php

来源: https://codeday.me/bug/20191202/2085581.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值