我需要有关jquery脚本的帮助,我在分页中使用它来创建延迟加载/无限滚动。
加载正在工作,但是,每当加载新数据时,它都会显示1,最后,如果没有更多的数据,它会无限地加载1111。我需要帮助来摆脱1,并停止加载,如果没有更多的数据从数据库。
我的脚本:
<script type="text/javascript">
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() >= $(document).height()) {
var last_id = $(".post-id:last").attr("id");
loadMoreData(last_id);
}
});
function loadMoreData(last_id){
$.ajax(
{
url: 'LoadingData1.php?last_id=' + last_id,
type: "get",
beforeSend: function()
{
$('.ajax-load').show();
}
})
.done(function(data)
{
$('.ajax-load').hide();
$("#load-data").append(data);
})
.fail(function(jqXHR, ajaxOptions, thrownError)
{
alert('There's an issue with Loading...');
});
}
</script>我的LoadingData1.php代码
<?php
$newid = $_GET['last_id'];
$not = $db -> prepare('SELECT * FROM orderinhomeonlinecall WHERE id < ? AND cuid=? ORDER BY id DESC LIMIT 10');
$not -> bind_param('ii', $newid, $uid);
$not -> execute();
$result = $not->get_result();
$json = include('data1.php');
echo json_encode($json);
?>我的data1.php代码:
<?php while ($row = $result->fetch_array()) {
$id1 = $row['id'];
$orderId1 = $row['orderId'];
$serviceName1 = $row['serviceName'];
$totalprice1 = $row['totalprice'];
$orderStatus1 = $row['orderStatus'];
?>
<a href="booktype/orderdetails.php?orderId=<?php echo $orderId1; ?>" class="post-id" id="<?php echo $id1 ?>">
<?php switch ($orderStatus1) {
case 1: ?>
<i class="fa fa-circle color-gray2-dark"></i>
<span><?php echo $serviceName1; ?></span>
<strong><?php echo $ctype1; ?></strong>
<em class="bg-gray2-light font-11">₦<?php echo $totalprice1; ?></em>
<?php break;
case 2:
case 3:
case 4:
case 5:
?>
<i class="fa fa-circle color-blue2-dark"></i>
<span><?php echo $serviceName1; ?></span>
<strong><?php echo $ctype1; ?></strong>
<em class="bg-blue2-light font-11">₦<?php echo $totalprice1; ?></em>
<?php break;
case 6:
?>
<i class="fa fa-circle color-green2-dark"></i>
<span><?php echo $serviceName1; ?></span>
<strong><?php echo $ctype1; ?></strong>
<em class="bg-green2-light font-11">₦<?php echo $totalprice1; ?></em>
<?php break;
case 10:
case 11:
?>
<i class="fa fa-circle color-red2-dark"></i>
<span><?php echo $serviceName1; ?></span>
<strong><?php echo $ctype1; ?></strong>
<em class="bg-red2-light font-11">₦<?php echo $totalprice1; ?></em>
<?php
break;
} ?>
<i class="fa fa-angle-right"></i>
</a>
<?php
}
?>任何帮助都将不胜感激。
发布于 2020-01-11 20:43:34
正如我在评论中所说的,json代码与你的php无关,
在while循环中,您需要向ajax控件显示一个类或id。
最终解决方案应如下所示:
你的php代码:
$newid = $_GET['last_id'];
$not = $db -> prepare('SELECT * FROM orderinhomeonlinecall WHERE id < ? AND cuid=? ORDER BY id DESC LIMIT 10');
$not -> bind_param('ii', $newid, $uid);
$not -> execute();
$result = $not->get_result();
include('data1.php');你的while循环:
<?php
while ($row = $result->fetch_array()) {
$id1 = $row['id'];
$orderId1 = $row['orderId'];
$serviceName1 = $row['serviceName'];
$totalprice1 = $row['totalprice'];
$orderStatus1 = $row['orderStatus'];
?>你的html in循环:
<div class="post-id" id="<?php echo $id1; ?>">
/*Do your other things in here */
</div>
<?php } ?>和ajax代码:
loaded = true; //Initialize
$(window).scroll(function() {
if(($(window).scrollTop() == $(document).height() - $(window).height()) && loaded) { //Add in condition
var last_id = $(".post-id:last").attr("id");
loadMoreData(last_id);
}
});
function loadMoreData(last_id){
loaded = false; //False at start ajax call
$.ajax({
url: '/loadMoreData1.php?last_id=' + last_id,
type: "get",
beforeSend: function()
{
$('.ajax-load').show();
}
})
.done(function(data)
{
$('.ajax-load').hide();
$("#post-data").append(data);
loaded = true; //true to load data
})
.fail(function(jqXHR, ajaxOptions, thrownError)
{
alert('No response...');
loaded = true;
});
}https://stackoverflow.com/questions/59693929
复制相似问题