doucong1268 2016-12-14 10:18
浏览 35
已采纳

为什么这段代码会给我重复的结果?

This is probably really easy and I'm probably gonna kick myself up the arse after this, but I have the following code which displays either html or json of data from a table and returns it.

<?php
session_start();
$base = dirname(dirname(__FILE__));
include($base."/include/db.php");

global $conn;
$trees = [];
$treeBoxes = [];
if(isset($_SESSION['clientId'])) {
    $clientId = $_SESSION['clientId'];
    $query = $conn->prepare("SELECT * FROM ct_trees WHERE client_id=?");
    $query->bind_param('i', $clientId);
    $query->execute();
    $result = $query->get_result();
    if($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $tree_id = $row['id'];
            $tree_name = $row['name'];
            $query = $conn->prepare("SELECT id FROM ct_connections WHERE tree_id=?");
            $query->bind_param('i', $tree_id);
            $query->execute();
            $result2 = $query->get_result();
            $connections = $result2->num_rows;
            array_push($treeBoxes, '<span class="checkbox abc-checkbox abc-checkbox-success">',
            '<input id="tree'.$tree_id.'" type="checkbox" rel="'.$tree_id.'">',
            '<label for="tree'.$tree_id.'">'.$tree_name.'</label>',
            '</span>');
            array_push($trees, ["id" => $tree_id, "name" => $tree_name, "connections" => $connections]);
            if(isset($_GET['json'])) {
                echo json_encode($trees);
            } else {
                echo join("", $treeBoxes);
            }
        }
    }
}
?>

Now let's say for example, we want the json result, I'm getting the following string:

[{"id":1,"name":"My Tree","connections":4360}][{"id":1,"name":"My Tree","connections":4360},{"id":4,"name":"Another Tree","connections":0}]

Now for some reason, it's giving me the first result in one array, and then the same result, but with the other rows, in a separate array.

  • 写回答

1条回答 默认 最新

  • doujiu9307 2016-12-14 10:22
    关注

    Fixed it, I knew it'd be silly:

    <?php
    session_start();
    $base = dirname(dirname(__FILE__));
    include($base."/include/db.php");
    
    global $conn;
    $trees = [];
    $treeBoxes = [];
    if(isset($_SESSION['clientId'])) {
        $clientId = $_SESSION['clientId'];
        $query = $conn->prepare("SELECT * FROM ct_trees WHERE client_id=?");
        $query->bind_param('i', $clientId);
        $query->execute();
        $result = $query->get_result();
        if($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $tree_id = $row['id'];
                $tree_name = $row['name'];
                $query = $conn->prepare("SELECT id FROM ct_connections WHERE tree_id=?");
                $query->bind_param('i', $tree_id);
                $query->execute();
                $result2 = $query->get_result();
                $connections = $result2->num_rows;
                array_push($treeBoxes, '<span class="checkbox abc-checkbox abc-checkbox-success">',
                    '<input id="tree'.$tree_id.'" type="checkbox" rel="'.$tree_id.'">',
                    '<label for="tree'.$tree_id.'">'.$tree_name.'</label>',
                    '</span>');
                array_push($trees, ["id" => $tree_id, "name" => $tree_name, "connections" => $connections]);
            }
            //Moved echo outside of while loop.
            if(isset($_GET['json'])) {
                echo json_encode($trees);
            } else {
                echo join("", $treeBoxes);
            }
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探