jQuery页面滚动不断重复来自mysql的数据

时间:2019-05-20 06:19:57

标签: php jquery ajax

我正在使用jquery页面滚动来显示mysql表中的数据。但是,当我滚动页面时,我看到数据重复出现,这意味着具有相同参数的相同查询会重复出现随机次数。这是我的代码:

details.php

<?
$sql = "SELECT MAX(slno) as mslno, Grade FROM table1 WHERE (SaleNo LIKE '$sale%') GROUP BY Grade ORDER BY slno ASC LIMIT 50";
$query = $connection->query($sql);
if($query->num_rows > 0){ 
while($row = $query->fetch_assoc()){
 $postID = $row["mslno"];
?>
<!--- Table to display records -->
<?php } ?>
<div class="load-more" lastID="<?php echo $postID; ?>" style="display: none;">
  <img src="loading.gif"/>
</div>
<?php } ?>

<script type="text/javascript">
$(document).ready(function(){
    $('body').on('touchmove', onScroll);
    $(window).on('scroll', onScroll);

    function onScroll(){
        var lastID = $('.load-more').attr('lastID');
        var sale = $('#hidsale').val();
        if ($(window).scrollTop() >  $(document).height() - $(window).height() - 100 && (lastID != 0)){
            $.ajax({
                type:'POST',
                url:'getData.php',
                data: 'sale='+sale+'&lastID='+lastID,
                beforeSend:function(){
                    $('.load-more').show();
                },
                success:function(html){
                    $('.load-more').remove();
                    $('#postList').append(html);
                }
            });
        }
    };
});

这是getData.php文件:

<?php
if(!empty($_POST["lastID"])){
require_once 'db.php';
$sale = $_POST['sale'];
$lastID = $_POST['lastID'];

$showLimit = 10;

$sql1 = "SELECT COUNT(*) as num_rows FROM table1 WHERE (SaleNo LIKE '$sale%') AND slno > '$lastID' ORDER BY slno ASC";
$queryAll = $connection->query($sql1);
$rowAll = $queryAll->fetch_assoc();
$allNumRows = $rowAll['num_rows'];

$sql2 = "SELECT MAX(slno) as mslno, Grade FROM table1 WHERE (SaleNo LIKE '$sale%') AND slno > '$lastID' GROUP BY Grade ORDER BY slno ASC LIMIT " .$showLimit;
$query = $connection->query($sql2);
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
    $postID = $row["mslno"]; ?>
<!-- Table data -->
<?php } ?>
<?php if($allNumRows > $showLimit){ ?>
<div class="load-more" lastID="<?php echo $postID; ?>" style="display: none;">
    <img src="loading.gif"/>
</div>
<?php }else{ ?>
<div class="load-more" lastID="0" style="padding: 4px; margin:4px; width: 98%; display: block; line-height: 20px; border: 1px solid red; text-align: center;">
    <strong>End of records!</strong>
</div>
<?php } }else{ ?>
<div class="load-more" lastID="0" style="padding: 4px; margin:4px; width: 98%; display: block; line-height: 20px; border: 1px solid red; text-align: center;">
    <strong>No Records Found!</strong>
</div>

对getData.php文件的第二个查询重复相同的slno随机次数,有时假设slno = 51,它重复两次,然后在slno = 61之后,它重复5次。抱歉,我已经粘贴了整个代码,但是我确实已经修复,无法找到解决方案。

0 个答案:

没有答案