douqin1932 2012-06-07 18:29
浏览 67
已采纳

json_encode在循环中不起作用

I have a json_encode in my php script which seems to fail the Jquery side of things if the json_encode is called inside a while loop.

When i view the result in a browser window other than the page i need the result in i can see that it does actually does work.

For example:

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

}

and the output in the browser is:

{"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}

In the jQuery response this is actually blank but if I leave the echo json_encode($data); just outside the while loop the jQuery picks up the response, however only 1 record - being the last record in the loop.

I need the jQuery to pick up all results from the loop.

Here's the 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>");
            }
        }
    });

Any ideas?

Thanks in advance

  • 写回答

8条回答 默认 最新

  • douqian1835 2012-06-07 18:32
    关注

    JSON needs to all be on one array or object. You need to append each row to an array, then json_encode that.

    $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);
    

    Then in JavaScript, you will have an array of objects, that you can loop through.

    $.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>");
             }
            });
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划