响应后在HTML中添加未知文本

时间:2019-01-28 07:34:09

标签: javascript php jquery ajax infinite-scroll

我一直在尝试实现无限滚动,在Ajax中成功调用之后,当我将结果附加到HTML时,它总是给我不必要的数字1,如您在图片中看到的,我不知道在哪里它来自 enter image description here
enter image description here

Pagination.php

<?php
include 'connection.php';

// header('Content-Type: application/json');

$pageno = $_POST['pageno'];

$no_of_records_per_page = 5;
$offset = ($pageno-1) * $no_of_records_per_page;

$sql = "SELECT *, 
  posts.id AS post_id, 
  posts.created_at AS post_created_at 
  FROM posts 
  LEFT JOIN users_account 
  ON posts.user_id = users_account.id 
  LEFT JOIN users_personal_info
  ON users_account.id = users_personal_info.user_id
  ORDER BY post_id DESC LIMIT {$offset}, {$no_of_records_per_page}";
$stmt = $connect->prepare($sql);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_OBJ);
$post_count = $stmt->rowCount();
if ($post_count > 0) {
    $results = include('data.php');
    echo $results;
} else {
    echo 'maxed';
}

Index.php

<?php


include 'connection.php'; 

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <?php include 'mda_header.php' ?>
  <style>

  #loader{
      display: block;
      margin: auto;
  }
</style>
</head>
<body>

<div class="container">
    <?php
        $sql = "SELECT *, 
          posts.id AS post_id, 
          posts.created_at AS post_created_at 
          FROM posts 
          LEFT JOIN users_account 
          ON posts.user_id = users_account.id 
          LEFT JOIN users_personal_info
          ON users_account.id = users_personal_info.user_id
          ORDER BY post_id DESC LIMIT 5";
        $stmt = $connect->prepare($sql);
        $stmt->execute(); 
        $rows = $stmt->fetchAll(PDO::FETCH_OBJ);
        ?>
        <?php include 'data.php' ?>
  <div id="response"></div>
</div>
  <input type="hidden" id="pageno" value="1">
  <img id="loader" src="https://i.redd.it/ounq1mw5kdxy.gif">
  <script src="jquery.inview.min.js"></script>
  <script>
    $(document).ready(function(){
      $('#loader').on('inview', function(event, isInView) {
          if (isInView) {
              var nextPage = parseInt($('#pageno').val()) + 1;
              console.log(nextPage);
              var total = parseInt($('#total').val());
              $.ajax({
                  type: 'POST',
                  url: 'pagination.php',
                  data: { pageno: nextPage, total: total },
                  success: function(response){
                    if (response != 'maxed') {
                      $('#response').append(response);
                      $('#pageno').val(nextPage);
                    } else {
                      $('#loader').hide();
                    }
                  }
              });
          }
      });
  }
  </script>
</body>
</html>

这是获取结果的地方,我在代码中看不到任何数字

data.php

<?php foreach ($rows as $row): ?>
      <div class="media">
        <img class="align-self-start mr-3 rounded-circle" src="uploads/<?= $row->image_upload ?>" height="55" width="55" alt="Generic placeholder image">
        <div class="media-body">
          <h5 class="mt-0">
            <a href="mdaProfileView.php?user=<?= $row->username ?>"
            data-post-author="<?= $row->username ?>" 
            data-placement="right" 
            data-container="body" 
            data-toggle="popover" 
            data-trigger="hover"
            data-html="true" 
            title="<b><?= ucfirst($row->fname) ?> <?= ucfirst($row->lname) ?>, <?= $row->age ?></b>" 
            data-content="<p class='text-muted'><?= empty($row->description) ? 'No description' : $row->description ?></p>"
            >

              <?= $row->username ?>
            </a>

            </h5>
          <small class="text-muted">
            <i>
              <i class="fa fa-clock-o"></i> 
              <span class="time" data-time-post-id="<?= $row->post_id ?>"><?= time_elapsed($row->post_created_at) ?></span>
            </i>
          </small>

          <p><small class="text-muted post-body"><?= $row->body ?></small></p>
           <?php  
           $like_stmt = $connect->prepare("SELECT *, COUNT(*) AS total_likes FROM likes WHERE post_id = {$row->post_id}");
           $like_stmt->execute();
           $like_row = $like_stmt->fetch(PDO::FETCH_OBJ);

           $comment_stmt = $connect->prepare("SELECT *, COUNT(*) AS total_comments FROM comments WHERE post_id = {$row->post_id}");
           $comment_stmt->execute();
           $comment_row = $comment_stmt->fetch(PDO::FETCH_OBJ);
           ?>
          <ul class="list-inline">
           <li class="list-inline-item">
            <small class="text-muted">
              <span class="likes_total_<?= $row->post_id ?>">
                <?= $like_row->total_likes ?></span><?= $like_row->total_likes > 1 ? ' Likes' : ' Like' ?>
            </small>
           </li>
           <li class="list-inline-item">
            <small class="text-muted">
              <span class="comments_total_<?= $row->post_id ?>">
                <?= $comment_row->total_comments ?>
              </span>
              <?= $comment_row->total_comments > 1 ? ' Comments' : ' Comment' ?>
            </small>
           </li>
           <li class="list-inline-item float-right">
            <small class="text-muted">25 Shares</small>
           </li>
         </ul>
           <ul class="list-inline">
            <li class="list-inline-item">
             <small>
                <?php  
                $user_id = user()->user_original_id;
                $isUserLiked_stmt = $connect->prepare("SELECT * FROM likes WHERE post_id = {$row->post_id} AND user_id = {$user_id}");
                $isUserLiked_stmt->execute();
                ?>
                <a 
                class="social-icon text-xs-center like like_button_<?= $row->post_id ?><?= $isUserLiked_stmt->rowCount() ? ' bold' : '' ?>" 
                data-user-id="<?= user()->user_original_id ?>" 
                data-author="<?= $row->username ?>" 
                data-post-id="<?= $row->post_id ?>" 
                data-type="post"
                href="javascript:void(0)">
                <i class="fa fa-thumbs-up up"
                ></i> Like</a>
              </small>
           </li>
            <li class="list-inline-item">
              <!-- comment_button -->
             <small><a class="social-icon text-xs-center comment" 
              href="javascript:void(0)" 
              data-post-id="<?= $row->post_id ?>" 
              data-toggle="modal" 
              data-target="#exampleModal"><i class="fa fa-comment"></i> Comment</a></small>
           </li>
            <li class="list-inline-item float-right">
             <small><a class="social-icon text-xs-center" href="javascript:void(0)"><i class="fa fa-share"></i> Share</a></small>
           </li>
          </ul>
        </div>
      </div>
   <?php endforeach; ?> 

1 个答案:

答案 0 :(得分:2)

您只需要一个包含-您也不需要回显结果,您可能会发现$results1来表示include()成功。更改

$results = include('data.php');
echo $results;

include('data.php');

如果所包含的脚本使用echo将数据传回,则仅需要分配和return