javascript - 向下滚动时自动加载更多仅加载前 2 页
问题描述
我在页面向下滚动时自动加载更多。我的 jQuery/ajax 正在工作,但它只在向下滚动时自动加载前 2 页。有更多页面/记录,但在第二页加载后卡住了。
我无法理解这个问题。有人请帮帮我。我的 php 和 java 代码如下
只是上下移动相同的移动线,但没有用。
<?php
$pxe = $_GET['pname'];
$sxe = $_GET['sname'];
?>
$(document).ready(function(){
var is_ajaxed = false;
function getresult(url) {
$.ajax({
url: url,
type: "GET",
data: {rowcount:$("#rowcount").val()},
beforeSend: function(){
$('#loader-icon').show();
},
complete: function(){
$('#loader-icon').hide();
},
success: function(data){
$("#faq-result").append(data);
},
error: function(){}
});
}
$(window).scroll(function(){
if ($(window).scrollTop() >= ($(document).height() - $(window).height()-900) && is_ajaxed == false){
if($(".pagenum").val() <= $(".total-page").val()) {
var pagenum = parseInt($(".pagenum").val()) + 1;
var pname = "<?php echo $pgianame; ?>";
var sname = "<?php echo $stianame; ?>";
getresult('sellers_forum_page_posts_getresult.php?page='+pagenum+'&pname='+pname+'&sname='+sname);
is_ajaxed = true
}
}
});
});
getresult.php
<?php
include('inc/db.php');
$perPage = 10;
$sql = "SELECT * from posts";
$allrows = $dba3->query($sql);
$allrowscount = mysqli_num_rows($allrows);
$pages = ceil($allrowscount/$perPage);
$page = 1;
if(!empty($_GET["page"])) {
$page = $_GET["page"];
}
$start = ($page-1)*$perPage;
if($start < 0) $start = 0;
$query = $sql." limit ".$start.",".$perPage;
$faq = $dba3->query($query);
if(empty($_GET["rowcount"])) {
$_GET["rowcount"] = mysqli_num_rows($faq);
}
$output = '';
if(!empty($faq)) {
$output .= '<input hidden class="pagenum" value="'.$page.'" />';
$output .= '<input hidden class="total-page" value="'.$pages.'" />';
while ($row = $faq->fetch_assoc()) {
$output .= $row["ename"];
} }
print $output;
?>
没有错误只是加载器图像不断移动
解决方案
关键是在应用限制之前从初始 sql 中获取行数,例如:
$perPage = 10;
$sql = "SELECT * from posts";
$allrows = $dba3->query($query);
$allrowscount = mysqli_num_rows($allrows);
$pages = ceil($allrowscount/$perPage);
$page = 1;
if(!empty($_GET["page"])) {
$page = $_GET["page"];
}
$start = ($page-1)*$perPage;
if($start < 0) $start = 0;
$query = $sql . " limit " . $start . "," . $perPage;
$faq = $dba3->query($query);
推荐阅读
- swift - 带有成功数组的通用结果枚举
- c# - 拆分后获取“System.String[]”而不是实际值。
- python - scipy.curve_fit 返回的不合逻辑的参数
- css - 如何增加下拉选择选项字段的高度?
- javascript - React 获取数据丢失
- python - 传递带有错误消息的字典并打印错误消息
- r - chart_Series:来自脚本时没有情节
- kubernetes - 如何使用出口网络策略允许访问 kubernetes api?
- html - ReactJS - 如何解析 JSON 元素中的 JSX 变量
- oracle - 许多人收到 SMTP 电子邮件,而其他人则没有