循环时json_encode内部不起作用


json_encode not working inside while loop

我的php脚本中有一个json_encode,如果在while循环中调用json_encode,它似乎会使Jquery方面失败。

当我在页面以外的浏览器窗口中查看结果时,我需要结果,我可以看到它确实有效。

例如:

while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];
    $data = array("id" => $id,
    "title" => $title,
    "success" => true
    );
echo json_encode($data); // Inside the While Loop
}

浏览器中的输出是:

{"id":"15","title":"Advanced Booking Examples","success":true}
{"id":"14","title":"Advanced Booking Fields","success":true}
{"id":"11","title":"Add New Driver","success":true}
{"id":"10","title":"Registering a Driver '/ Add New Vehicle","success":true}
{"id":"8","title":"Dispatch Controls","success":true}
{"id":"7","title":"Troubleshooting Driver Device Checklist","success":true}
{"id":"6","title":"Requirements Checklist","success":true}

在 jQuery 响应中,这实际上是空白的,但是如果我将echo json_encode($data);留在 while 循环之外,jQuery 会选取响应,但只有 1 条记录 - 这是循环中的最后一条记录。

我需要jQuery从循环中获取所有结果。

这是 JS

$.ajax({ // Send it off for prcessing
        type: "POST",
        dataType: 'json',
        url: "includes/auto_suggest.php",
        data: {
            q: inputString
        },
        success: function (result) {
            if (result.success) {             
                var id = result.id;
                var title = result.title;                    
                $('#auto_id').append("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");              
        $('#auto_title').prepend("<p>" + title + "</p>");
            }
        }
    });

有什么想法吗?

提前致谢

JSON 需要全部位于一个数组或对象上。 您需要将每一行附加到数组中,然后json_encode

$data = array();
while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];
    $data[] = array(
      "id" => $id,
      "title" => $title,
      "success" => true
    );
}
echo json_encode($data);

然后在 JavaScript 中,你将有一个对象数组,你可以循环访问。

$.ajax({ // Send it off for prcessing
    type: "POST",
    dataType: 'json',
    url: "includes/auto_suggest.php",
    data: {
        q: inputString
    },
    success: function (result) {
        $.each(result, function(){
          if (this.success) {
            var id = this.id;
            var title = this.title;
            $('#auto_id').append("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");
            $('#auto_title').prepend("<p>" + title + "</p>");
         }
        });
    }
});

好吧,您没有发送有效的 JSON。将对象存储在数组中,然后在循环后json_encode该数组。

$data = array();
while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];
    $data[] = array("id" => $id,
        "title" => $title,
        "success" => true
    );
}
echo json_encode($data);

然后,您需要更改 JavaScript 代码以正确处理数组而不是对象。由于这里没有人知道你到底打算做什么,你必须自己做或提供更多信息。

您需要

将结果数据添加到数组中并调用json_encode一次,如下所示:

$data = array();
while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];
    $data[] = array("id" => $id,
    "title" => $title,
    "success" => true
    );
}
echo json_encode($data);

这将更改您输出的 JSON(因为当前您的 JSON 无效),您现在必须在 jQuery 回调中循环。

当该行位于循环内时返回的 JSON 整体无效。它是多个 JSON 字符串在一起。您需要将它们分开。

我会创建一个数组,并在循环中将每个$data添加到该数组中。然后,在循环之外,json_encode它并回显它。这将创建一个 JSON 字符串以返回到您的 JavaScript。

也许...

$data = array();
while( WHATEVER ){
    // other code
    $data[] = array('id' => $id, 'title' => $title, 'success' => true);
}
echo json_encode($data);

将所有信息存储在一个数组中,然后对该数组进行编码。

你不能将

多个这样的对象传递给jQuery。放入数组并在 while 循环完成后打印对象数组。

您需要在 while 循环之外对 json 进行编码。 它将返回单个 json,这实际上是正确的

while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];
    $data[] = array("id" => $id,
    "title" => $title,
    "success" => true
    );

}
echo json_encode($data); 

您的 JSON 输出无效。试试这个完整的代码(编辑):

$result = Array();
while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];
    $data = array("id" => $id,
        "title" => $title,
    );
    $result[] = $data;
}
echo json_encode(Array('data'=>$result,'success'=>true)); // Outside the While Loop

在你的JavaScript代码中

$.ajax({ // Send it off for prcessing
        type: "POST",
        dataType: 'json',
        url: "includes/auto_suggest.php",
        data: {
            q: inputString
        },
        success: function (results) {
            if (results.success) {
                for(var i in results.data) {
                    var result = results.data[i];             
                    var id = result.id;
                    var title = result.title;                    
                    var $newfield = $("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");              
                    $newfield.prepend("<p>" + title + "</p>");
                    $('#auto_title').append($newfield);
                }
            }
        }
    });