douhuxi4145 2015-10-09 22:15
浏览 22

PHP / JS Unlimited滚动加载来自MySQL的所有数据

I was working on Unlimited scroll for my website using PHP and JavaScript. I almost made it but there was a problem In my code, after scrolling to the bottom of the page my script should load 15 articles every time I scroll to the bottom and after its done It stops loading more posts, but instead it loads all of the articles and then stops the script when all of the posts are loaded.

My JavaScript code:

<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 File for loading the posts through 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 } ?>
  • 写回答

1条回答 默认 最新

  • douba1904 2015-10-09 22:37
    关注

    Looks to me like your LIMIT syntax is backwards

    LIMIT 15,".$countBlop.

    should be

    LIMIT ".$lastIndex.",15

    As is now it would be starting at record 15 and then continuing to load until you reach the total count(as defined by $countBlop)

    You will also need to have a way for jQuery to pass the last index to the ajax call.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)