duanmu1736 2017-04-09 01:12
浏览 42
已采纳

有没有办法计算PHP发送回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.

Like in my 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:

<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.

So basically, the number of the open tasks, aka the number of the while loops, is the number of the echos. What is the best way to grab this number so I can show in the html how many tasks are listed here?

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.

Thanks guys!

  • 写回答

2条回答 默认 最新

  • dongliuxia9495 2017-04-09 04:20
    关注

    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. You don't need a loop to do this, just use PDO's fetchAll method. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多