PHP 无限滚动不加载更多帖子

时间:2021-01-27 03:02:41

标签: javascript php jquery ajax

我正在尝试让这个无限卷轴工作。我不擅长 php 或 javascript,这对我没有帮助。

它会加载前 20 个帖子,但无论出于何种原因,当我向下滚动以加载更多帖子时,我总是收到一条错误消息,指出“没有更多帖子可用”。

这里是: index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Home Page</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
<link href="css/main.css" rel="stylesheet" type="text/css" media="all">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

<body>
<div class="container-fluid no-gutter">
  <nav class="navbar navbar-dark bg-dark">
    <div class="container">
      <h1 class="navbar-brand">Posts</h1>
    </div>
  </nav>
  <div class="container padding">
    <div class="row">
      <div class="col-3">

      </div>
      <div class="main col-9" id="post-list">
        <input type="hidden" name="total_count" id="total_count" value="<?php echo $total; ?>" />
        <?php
          include ("config/config.php");
          
            try {
              $conn = new PDO("mysql:host=$dbh;dbname=$dbn", $dbu, $dbp);
              $sql = "SELECT address, FROM_UNIXTIME(date/1000,'%M %D, %Y %h:%i:%s') as date, body FROM table_name";
              $total = $conn->query($sql)->rowCount();
              $sql = "SELECT row, address, FROM_UNIXTIME(date/1000, '%M %D, %Y %h:%i %p')as date, body FROM table_name LIMIT 20";

              foreach($conn->query($sql) as $row) {
                echo '<div class= "home">'; 
                echo '<div class= "address">' . $row["address"] . '</div>';
                echo '<div class= "body">' . $row["body"] . '</div>';
                echo '<div class= "date">' . $row["date"] . '</div>';
                echo '</div>';
              }
            }
            catch(PDOException $e) {
              echo "Error: " . $e->getMessage();
            }
            $conn = null;
        ?>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){
        windowOnScroll();
        $("div.address:contains(Var)").parent().removeClass("home");
                $("div.address:contains(Var)").parent().addClass("away");
});
function windowOnScroll() {
       $(window).on("scroll", function(e){
        if ($(window).scrollTop() == $(document).height() - $(window).height()){
           console.log('Scrolling')
            if($(".home").length < $("#total_count").val()) {
                var lastId = $(".home:last").attr("id");
                getMoreData(lastId);
            }
            else{
                console.log('No More posts aviliable')
            }
        }
    });
}

function getMoreData(lastId) {
       $(window).off("scroll");
    $.ajax({
        url: 'getMoreData.php?lastId=' + lastId,
        type: "get",
        beforeSend: function ()
        {
            $('.ajax-loader').show();
        },
        success: function (data) {
               setTimeout(function() {
                $('.ajax-loader').hide();
            $("#post-list").append(data);
            windowOnScroll();
               }, 10);
        }
   });
}
</script>
</body>
</html>

getMoreData.php

<?php
    include ("config/config.php");

    $conn = new PDO("mysql:host=$dbh;dbname=$dbn", $dbu, $dbp);
    $lastId = $_GET['lastId'];
    $sql = "SELECT row, address, FROM_UNIXTIME(date/1000, '%M %D, %Y %h:%i %p')as date, body FROM table_name WHERE `row` < '" .$lastId . "' LIMIT 20";

    foreach($conn->query($sql) as $row) {
      echo '<div class="home">'; 
      echo '<div class= "address">' . $row["address"] . '</div>';
      echo '<div class= "body">' . $row["body"] . '</div>';
      echo '<div class= "date">' . $row["date"] . '</div>';
      echo '</div>';
    }

?>

我花了几天时间试图让它产生各种结果。

请帮忙!哈哈...

1 个答案:

答案 0 :(得分:2)

尝试将 <input type="hidden" id="total_count" value="<?php echo $total?>"> 放在 PHP 代码之后。因为 $total 尚未初始化。