2011-11-18 128 views
0

我試圖代碼無盡的分頁和具有麻煩無盡分頁幫助jQuery的/ PHP

$(document).ready(function(){ 

     function lastPostFunc() 
     { 
      $('div#lastPostsLoader').html('<div class="load"><div class="label"><font color="black"><b>Loading more...</b></font></div></div>'); 
      $.post("cr/sc/scr.php?lastID="+$(".comment:last").attr("id"), 

      function(data){ 
       if (data != "") { 
       $(".comment:last").after(data);   
       } 
       $('div#lastPostsLoader').empty(); 

      }); 
     }; 

     $(window).scroll(function(){ 
      if ($(window).scrollTop() == $(document).height() - $(window).height()){ 

       lastPostFunc(); 
      } 
     }); 

    }); 

在這裏不用我的PHP

$result = mysql_query('SELECT * FROM xyz ORDER BY sy DESC LIMIT 15'); 

      while($row = mysql_fetch_object($result)) { 
       $i++; 
             echo "<div class='comment' id='" . $i. "'>"; 
       } 

和其他網頁抓取數據

$皮克= $ _GET ['lastID'];

$ i = $ pg; $ result = mysql_query('SELECT * FROM xyz ORDER BY sy DESC LIMIT'。$ pg。',15');

  while($row = mysql_fetch_object($result)) { 
       $i++; 
          echo "<div class='comment' id='" . $i . "'>"; 
} 

林在獲得評論的價值得到了一個問題:第2次之後最後:最後

我得到評論的15值:去年第1次事件加載後

我得到評論的15值事件負載

這是問題的IM預計30

和第3次的事件occures它給了30

和同proccess再次30,30,40,40,50,50

,而不是30,40,50,60,70,80

我試圖jQuery的生活(),我用$ _GET導致im從URL中提取

+0

我刪除了我的答案。我的壞,你的代碼完美的作品。你有沒有檢查你的評論:最後的ID是? id和get值有什麼區別嗎?在事件 – tuze

+0

IM剛開問題只 fireing其對PHP和數據庫完善 – Smit

回答

0

檢查以查看您是否正在將函數加載到其他位置,因爲該函數將加載並停在我的,並且唯一的方法是我可以讓它模仿您所說的是如果我加載函數再次在頁面加載,這將同時創建兩個電話。導致1-15和1-15以及頁面上的最後一個數字。然後在滾動它加載函數,因爲它應該和繼續加載,就像你也告訴它。

另外,請確保您正在檢查直接進入數據庫的文件(或者即使您使用了文章)。這是要求注射。

編輯:如果您有數據,您應該首先加載整個頁面(和過去)。然後加載滾動功能,如果頁面未滿,將不會加載。這就是爲什麼它停止了我。

EDIT2:這是我使用的代碼,以防萬一你想看到。我做了一個模擬數據庫,所以我不需要匹配一個db來測試。

<?php 
$scroller = 30; 

if (isset($_GET['hold']) && $_GET['hold'] == '1') { 
    $i = isset($_GET['lastID'])?(int)$_GET['lastID']:0; 
    $j = $i + $scroller; 
    //sleep(10); 
    while($i < $j) { // mimic a mysql call and spit it out. 
     ++$i; 
     echo "<div class='comment' id='${i}'>${i}</div>"; 
    } 
    exit; 
} 
?> 
<html> 
<head> 
<script src="jquery-1.7.min.js" type="text/javascript"></script> 

<script type="text/javascript">  
// I was doing something else with this, but stripped out the code 
var scroller = <?php echo $scroller;?>; 
$(document).ready(function(){ 
    function lastPostFunc() 
    { 
     // this assumes there are already comments on the page, so put a dummy comment 
     var last = $(".comment:last").attr("id"); 

     $('div#lastPostsLoader').html('<div class="load"><div class="label"><font color="black"><b>Loading more...</b></font></div></div>'); 
     $.post("<?php echo $_SERVER['PHP_SELF']; ?>?hold=1&lastID="+last, 

     function(data){ 
      if (data != "") { 
       $(".comment:last").after(data);   
      } 
      $('div#lastPostsLoader').empty(); 
     }); 
    }; 

    $(window).scroll(function(){ 
     if ($(window).scrollTop() == $(document).height() - $(window).height()){ 
      lastPostFunc(); 
     } 
    }); 
    lastPostFunc(); 
}); 
</script> 
</head> 
<body> 

<div class="comment_container"> 
    <div class="comment" id="0"></div> 
    <div id="lastPostsLoader"></div> 
</div> 

</body> 
</html> 
+0

我在主索引文件只JS我 西港島線剛剛得到scr.php 我不是讓你的結果,你的意思是謂事件被解僱2次 – Smit

+0

當我實現相同的代碼,它只發射一次,如此1-15。然後,當我加起來30時,它是1-30,頁面上有30。觸發滾動,所以它是1-30,然後滾動到30觸發31-60。所以基本上,除了時間不夠長之外,它的工作。我能得到1-15,1-15的唯一方法是如果代碼被激發兩次。 – craniumonempty

+0

如果您在同一頁面運行php sql調用(您可能不是,但在測試時執行此操作),請確保在調用它時退出。無論哪種方式,測試該頁面以外的jquery,看看它是否加載正常。 – craniumonempty