PHP/JS无限滚动加载所有数据从MySQL在一个滚动


PHP/JS Unlimited scroll loading all data from MySQL in one scroll

我正在为我的网站使用PHP和JavaScript无限滚动工作。我几乎做到了,但有一个问题在我的代码,滚动到页面底部后,我的脚本应该加载15篇文章,每次我滚动到底部,完成后,它停止加载更多的帖子,而是加载所有的文章,然后停止脚本当所有的帖子加载。

JavaScript代码:

<script>
// Unlimited Scroll
$(document).ready(function() {
    $('.post-load').hide();
    var load = 0;
    var nbr = "<?php echo mysql_num_rows($sql); ?>";
    $(window).scroll(function() {
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
            $('.post-load').show();
            load++;
            if (load * 15 > nbr) {
                $('.post-load').hide();
            } else {
                $.post("extensions/ajax.php", {
                    load: load
                }, function(data) {
                    $('.lb').append(data);
                    $('.post-load').hide();
                });
            }
        }
    });
});

extensions/ajax.php

<?php 
include 'connect.php';
$load = htmlentities(strip_tags($_POST['load'])) * 2;
$sql = mysql_query("SELECT * FROM articles ORDER BY postNum DESC");
$countBlop = mysql_num_rows($sql);
$queryL = mysql_query("SELECT * FROM articles ORDER BY postNum DESC LIMIT 15,".$countBlop."");
while ($posts = mysql_fetch_assoc($queryL)) {
?>
<div class="article">Echo the posts...</div>
<?php } ?>

在我看来你的LIMIT语法是向后的

LIMIT 15,".$countBlop.

应该

LIMIT ".$lastIndex.",15

现在,它将从记录15开始,然后继续加载,直到达到总数(由$countBlop定义)

您还需要有一种方法让jQuery将最后一个索引传递给ajax调用