簡體   English   中英

從表檢索的條目是否重復? PHP

[英]Entries retrieved from table are being duplicated? PHP

我的網站上有一個結果頁面,我在向下滾動時使用AJAX返回更多的結果,但是我的問題是,它拉結果時,似乎多次拉相同的結果? 我不知道是什么原因造成的,有人可以看到我做錯了什么嗎?

AJAX

$(window).scroll(function () {
    if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10)  {
         var number = $(".directory").children().length;
        $.ajax({
           type: "POST",
           url: "getentries.php",
           data: "count="+number,
           success: function(results){
             $('.directory').append(results);
           }
         });



    } else {}
});

PHP

$result = mysql_query("SELECT * FROM directory LIMIT {$_POST['count']},12");

$c = 1;
while($row = mysql_fetch_array($result))
  {
  echo '<div class="entry';
            if (($c % 4) == 1) echo ' alpha ';
            echo 'ALL THE DATA IS GOING HERE';      
            $c++;
  }

問題很可能是在您滾動時觸發了多個ajax調用。 設置定時事件監聽器,如下所示:

didScroll = false;
$(window).scroll(function() {
    didScroll = true;
});

setInterval(function() {
    if ( didScroll ) {
        didScroll = false;
        if(($(document).height() - $(window).height()) - $(window).scrollTop() < 100) {
            // load more results
        }
    }
}, 250);

本文介紹了為什么您的解決方案不是一個好主意: http : //ejohn.org/blog/learning-from-twitter/

因此,問題在於,一旦達到滾動閾值,您將多次調用ajax。 您需要在第一個ajax調用上添加一個標志,以便在ajax調用完成之前不會再次調用它。

ajaxInProgress = false;
if (!ajaxInProgress && ($(window).scrollTop() >= $(document).height() - $(window).height() - 10)) {
    ajaxInProgress = true;
    $.ajax({
       type: "POST",
       url: "getentries.php",
       data: "count="+number,
       success: function(results){
         ajaxInProgress = false;
         $('.directory').append(results);
       }
     });
}

問題在於您如何通過ajax調用傳遞數據。 在Jquery文檔中,您需要在數據中的javascript對象中放入數量和數量:

$.ajax({
       type: "POST",
       url: "getentries.php",
       data: {count: number},
       success: function(results){
         $('.directory').append(results);
       }
     });

解決此問題后,您的Mysql查詢應將正確的下一部分數據返回到您的視圖。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM