6

我正在尝试使用 AJAX 和 PHP 即时从我的表中获取 20 个结果(当用户滚动到底部时)。

到目前为止,我一直在关注...

AJAX

// Infinite Scroll
loadData( 0 );
//Hide Loader for Infinite Scroll
$('div.ajaxloader').hide();

});

function loadData ( last_id ) {
    var $entries = $('.directory'),
        $loader = $('.ajaxloader', $entries).show();
    $.get( '/getentries.php', { last_id : last_id }, function( data ) {
        $entries.append( data ).append( $loader.hide() );
        filterEntries();
    });
};


//Isotope filter - no changes to this code so I didn't include it

$(window).scroll(function () {
    if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
        $('div.ajaxloader').show('slow');
        loadData( $( '.directory item:last' ).data('id') )
    }

PHP

try {
  // Connect and create the PDO object
  $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
  $conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8

  // Define and perform the SQL SELECT query
  $sql = "SELECT * FROM `directory` WHERE user_active != ``";
  $result = $conn->query($sql);

  $sth = $conn->prepare("SELECT * FROM directory WHERE user_active != ''");
        $sth->execute(array(':county' => $county));

        $c = 1;
        while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
            echo '<div class="entry';
                 if (($c % 4) == 1) echo ' alpha ';
           // ECHO RESULT STYLES ETC HERE
        $c++;
        }

  $conn = null;        // Disconnect
}
catch(PDOException $e) {
  echo $e->getMessage();
}

现在我的 AJAX 和查询似乎工作正常,但我遇到的问题是它只是再次提取所有记录,而不是接下来的 20 条记录?

我以前从未这样做过,如果我听起来很天真,我很抱歉,但任何帮助将不胜感激!

4

3 回答 3

1

您需要在 mysql 查询中添加限制。请看下面的代码

// Infinite Scroll
loadData( 0 );
//Hide Loader for Infinite Scroll
$('div.ajaxloader').hide();

});

function loadData ( last_id ) {
    var $entries = $('.directory'),
        $loader = $('.ajaxloader', $entries).show();
    $.get( '/getentries.php?startp=0&endp=20', { last_id : last_id }, function( data ) {
        $entries.append( data ).append( $loader.hide() );
        filterEntries();
    });
};


//Isotope filter - no changes to this code so I didn't include it

$(window).scroll(function () {
    if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
        $('div.ajaxloader').show('slow');
        loadData( $( '.directory item:last' ).data('id') )
    }


PHP


try {
  // Connect and create the PDO object
  $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
  $conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8

  // Define and perform the SQL SELECT query
  $sql = "SELECT * FROM `directory` WHERE user_active != ``";
  $result = $conn->query($sql);

  $sth = $conn->prepare("SELECT * FROM directory WHERE user_active != '' limit $_REQUEST['startp'],$_REQUEST['endp']");
        $sth->execute(array(':county' => $county));

        $c = 1;
        while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
            echo '<div class="entry';
                 if (($c % 4) == 1) echo ' alpha ';
           // ECHO RESULT STYLES ETC HERE
        $c++;
        }

  $conn = null;        // Disconnect
}
catch(PDOException $e) {
  echo $e->getMessage();
}

谢谢里帕萨哈

于 2012-12-20T09:57:25.217 回答
0

SELECT * FROM foo WHERE id > $lastIndex ORDER BY id LIMIT 20;

编辑:哎呀。那是针对常规 mysql,而不是使用准备好的语句。基于php docs,您的答案应该如下所示:

$sql = "SELECT * FROM directory WHERE id > :lastIndex AND user_active != '' LIMIT 20";
$sth = $conn->prepare($sql);
$sth->execute(array(':lastIndex' => $lastIndex));

它的作用是在$lastIndex. 您需要做的就是将 lastIndex 的值(应该与页面上的结果数一致)与您的请求一起发送并相应地使用它。

或者,您可以执行相反的操作并将$lastIndex结果发送到客户端,以便在用户发出新请求时存储和使用 javascript。

于 2012-12-19T20:13:34.020 回答
0

好吧,简短的回答是为您的查询添加偏移量。就像分页一样。您只需跟踪您所在的页面并将查询偏移某个值。在这种情况下,您无需更改页面,而是在最后加载它。这是一个例子你可以谷歌它并阅读更多

于 2012-12-20T03:31:29.040 回答