简体   繁体   English

有没有办法计算PHP发送回jQuery的数据库结果数量?

[英]Is there a way to count how many database results the PHP sends back to jQuery?

In my project, I use PHP to query the database(in my case, SELECT) and use while loop to fetch each row, and use it to echo back to ajax. 在我的项目中,我使用PHP查询数据库(在我的情况下,SELECT)并使用while循环来获取每一行,并使用它回显到ajax。

Like in my PHP: 就像我的PHP:

$sql = "SELECT * FROM tasks A WHERE A.open = '1'";

$stmt = $pdo->prepare($sql);

$stmt->execute();

while ($row = $stmt->fetch()){
    echo $row["orderDetails"];
}

In my HTML: 在我的HTML中:

<script>
    $(function(){
        $.ajax({
            method: "POST",
            url: "getDetails.php",
            success: function(backData){
                $("#details").html(backData);
            }
     });
<script>

Let's say I query the database for the open tasks in my task table where task.open=1, then inside of each open task, I echo back to my ajax about these tasks details, and my ajax will change the html of the div about the task details. 假设我在任务表中查询数据库中的open任务,其中task.open = 1,然后在每个打开的任务内部,我回复我的ajax关于这些任务的详细信息,我的ajax将更改div的html任务细节。

So basically, the number of the open tasks, aka the number of the while loops, is the number of the echos. 所以基本上,打开任务的数量,即while循环的数量,是回声的数量。 What is the best way to grab this number so I can show in the html how many tasks are listed here? 获取此数字的最佳方法是什么,以便我可以在html中显示此处列出了多少任务?

PS: I tried to catch this number by adding a while loop counter inside of the while loop, and add this number to an attribute of the div and echo back to ajax, however, I don't know why this echo will not take place in my end. PS:我试图通过在while循环中添加while循环计数器来捕获这个数字,并将此数字添加到div的属性并回显给ajax,但是,我不知道为什么不会发生这种回声在我的最后。

Thanks guys! 多谢你们!

I solved it by adding a div with a class attribute to the echo to contain this order details and then back to my html I used jQuery to count the div with that specific class and get the number. 我通过向echo添加一个带有class属性的div来解决它,以包含此订单详细信息然后返回到我的html我使用jQuery来计算该特定类的div并获取数字。 I get the hint from here: count div elements with the same id JQUERY But you are very welcome to post a better solution for this :D 我从这里得到提示: 计算具有相同ID的div元素JQUERY但是非常欢迎您为此发布更好的解决方案:D

What you want to do is send back a proper JSON response, then you can just use the length property to check how many items there are. 你想要做的是发送一个正确的JSON响应,然后你可以使用length属性来检查有多少项。 You don't need a loop to do this, just use PDO's fetchAll method . 您不需要循环来执行此操作,只需使用PDO的fetchAll方法即可 Since you are only selecting a single column, you can get it as an array of strings. 由于您只选择单个列,因此可以将其作为字符串数组获取。

<?php
$sql = "SELECT orderDetails FROM tasks WHERE open=1";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
header("Content-Type: application/json");
echo json_encode($array);

Now you have an array of strings, you can do what you want with it, including getting its length . 现在你有一个字符串数组,你可以用它做你想做的事情,包括获取它的长度

<script>
$(function(){
    $.ajax({
        method: "POST",
        url: "getDetails.php",
        success: function(backData) {
            // now you can work with backData as an object
            var count = backData.length;
            $("#details").append("<div>You have " + count + " results.</div>");
            for (var i = 0; i < count; i++) {
                $("#details").append("<div>" + backData[i] + "</div>"
            }
        }
    });
});
<script>

Also note you were missing a closing }) pair in your JavaScript code. 另请注意,您在JavaScript代码中缺少一对结束})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM