php - 如何使用 AJAX 使用查询的最后一个 ID 进行查询?

标签 php jquery mysql ajax

进一步解释:

这是我的 AJAX:

$(document).ready(
    function loadAll (param) {
        $.ajax({
            url: 'functions.php',
            type: 'POST',
            dataType: 'JSON',
            data: {task: "load", last_id: param},
            success: function(data) {
                $.each(data, function(i, attr) {
                    $("#result").append("<tr><td style='width:300px;'>" + attr.actor_id + "</td><td style='width:300px;'>" + attr.first_name + "</td>");
                })
            }
        })
    });
$(window).scroll(function () {
   if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
        //Perform the loadAll() function using the last ID of the last query performed
        //and then appending it to the #result
   }
});

在这里,我基本上是发送请求并将其附加到我的 html 中的 #result。然后,当用户一直向下滚动时,我希望该函数发送上次执行的查询的 last_id 并检索包含返回的新项目的新查询。

这是我的 PHP:

if (isset($_POST['task']) && $_POST['task'] == "load") {

    $last_id = isset($_POST['last_id'] ) ? (int)$_POST['last_id'] : 0; 

    $stmt = $connection->prepare("SELECT * FROM actor WHERE actor_id > (?) ORDER BY actor_id LIMIT 0, 30");
    $stmt->bind_param('i', $last_id);
    $stmt->execute();

    $result = $stmt->get_result();   

    $encode[] = array(); 

    while( $row = $result->fetch_assoc() ) {
        $encode[] = $row;
    }
        echo json_encode($encode);
}

此查询检查是否设置了“last_id”,如果没有,则将 last_id 设置为 0。这工作正常。

这是我的 HTML(如果重要的话):

<div class="container">
    <h1>Making an infinite scroll</h1>
    <p>Load posts below:</p>

    <div id="result"></div>
</div>

问题:如何使用 AJAX 使用查询的最后一个 ID 进行查询,然后使用最后一个 ID 进行另一个查询?

这是我设想的过程:

1- POST 请求,返回第一个查询 [actor_id 为 0、30 的 Actor ],将其附加到#result

2-将查询的最后一个id保存到一个变量中

3- 如果用户向下滚动到页面末尾,使用上次查询的最后一个id执行另一个查询[actor_id为30的actors,到60],然后追加它到#result div。

如有任何帮助,我们将不胜感激。

最佳答案

您可以将最后获取的 ID 与您的查询结果一起发送

while( $row = $result->fetch_assoc() ) {
    $encode[] = $row;
}

$last_id = $encode[count($encode)-1]['actor_id'];

echo json_encode( array('encoded' => $encode, 'last_id' => $last_id) );

然后在客户端(您的 jquery 代码),您只需这样做:

    var last_id = 0;

    function loadAll (param) {
        $.ajax({
            url: 'functions.php',
            type: 'POST',
            dataType: 'JSON',
            data: {task: "load", last_id: param},
            success: function(data) {
                results = data.encoded;
                last_id = data.last_id;
                $.each(results, function(i, attr) {
                    $("#result").append("<tr><td style='width:300px;'>" + attr.actor_id + "</td><td style='width:300px;'>" + attr.first_name + "</td>");
                })
            }
        })
    }

$(window).scroll(function () {
   if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
         loadAll(last_id);
        //Perform the loadAll() function using the last ID of the last query performed
        //and then appending it to the #result
   }
});

我不确定我的问题是否正确,但我想这是您问题的可能解决方案。

附言:

SELECT * FROM actor WHERE actor_id > (?) ORDER BY actor_id LIMIT 0, 30

有什么具体原因导致你不这样做吗

SELECT * FROM actor ORDER BY actor_id LIMIT (?), 30

关于php - 如何使用 AJAX 使用查询的最后一个 ID 进行查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20449465/

相关文章:

php - 用逗号(,)将数组数据存储到字符串中

php - 从 ajax 文件访问先前包含的(必需的)类方法

javascript - 保存所选元素的 ID

php - 添加另一个下拉列表到动态 php、Mysql 表单以及另外 2 个下拉列表

javascript - 隐藏提交按钮,直到选择文件

mysql - 尝试连接到托管在 Google Compute Engine Ubuntu VM 上的 SQL 服务器时如何解决此错误

php - 无法连接到 000webhost 中的数据库

php - 检测PHP中的空变量不起作用

PHP PDO - 多个语句 - 如果 1 条语句失败,则不执行另一条语句

mysql - 组合sql查询耗时太长