JSON编码多个MySQL行 [英] JSON encode for multiple MySQL rows

查看:134
本文介绍了JSON编码多个MySQL行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过一个简单的搜索框通过PHP(服务器端)和jQuery(前端)将多个MySQL JSON行输出到HTML表.我已经成功地将JSON的一行解析到表中,但是现在需要容纳多行JSON数据.

I'm working on outputting multiple MySQL JSON rows to an HTML table via PHP (server side) and jQuery (front end) through a simple search box. I've successfully managed to parse one row of JSON to the table but now need to accommodate multiple rows of JSON data.

我在搜索页面上的HTML表格如下所示:

My HTML table on the search page looks something like this:

ARTIST | TITLE | LOCATION 
U2     | One   | Ireland 
Foals  | Miami | UK

以及传递给它的JSON文件使用相似的标题:

and the JSON file passed to it utilises similar headings:

{"ARTIST":"Katy Perry","LOCATION":"United States", "TRACKTITLE":"Roar"}

我一直在研究代码一段时间,并引入了数组来遍历数据.它的工作原理与正确循环遍历MySQL行并成功地以JSON格式发送回多行数据(我已经在Firebug中解码了搜索页面)一样.问题是我的jQuery文件无法将其放入我的HTML表中,从而导致页面空白.我已经尝试了先前Stackoverflow问题中的各种数组方法,但无法完全正常工作,因为返回的每个返回结果都将返回"ARTIST","TRACKTITLE"和"LOCATION"字段

I've been playing around with the code for a while, incorporating arrays to loop through the data. This works in as much as it loops through the MySQL rows correctly and successfully sends multiple rows of data back in JSON format (I've decoded my search page in Firebug). The problem is that my jQuery file can't place it into my HTML table, resulting in a blank page. I've tried various methods of arrays from previous Stackoverflow questions but can't quite get it to work, being returned the 'ARTIST', 'TRACKTITLE' and 'LOCATION' fields with every returned result

从PHP文件提取:

$result = mysql_query($sql);
$data = array();
while ($array = mysql_fetch_assoc($result)) {
$data[] = $array;
}
echo json_encode($data);

编辑后包含jQuery提取和更具体的问题:

function(data) {
var sentData=JSON.parse(data)
for (var i = 0; i < sentData.ARTIST.length; ++i)
{
tr = $('<tr/>');
tr.append("<td>" + sentData.ARTIST + "</td>");
tr.append("<td>" + sentData.TRACKTITLE + "</td>");
tr.append("<td>" + sentData.LOCATION + "</td>");
$('#multiple').append(tr);
}
});

如何使用JSON.parse方法仅解析每个"ARTIST","TRACKTITLE"和"LOCATION"结果的对应值?这是返回的完整JSON字符串:

How can I parse only the corresponding values of each 'ARTIST', TRACKTITLE' and 'LOCATION' result using the JSON.parse method? This is the full returned JSON string:

[{"ARTIST":"Katy Perry","TRACKTITLE":"Roar","LOCATION":"United States"}, {"ARTIST":"U2","TRACKTITLE":"One","LOCATION":"Ireland"}]

推荐答案

尝试一下,我采用了您提供的javascript,并进行了如下更改. (另请参见底部的jsfiddle)

Try this I took the javascript you provided and changed as below. (see also jsfiddle at the bottom)

var sentData=JSON.parse('{"ARTIST":[["Katy Perry"], ["U2"]],"LOCATION":[["United States"], ["Ireland"]],"TRACKTITLE":[["Roar"], ["One"]]}')
for (var i = 0; i < sentData.ARTIST.length; ++i)
{
tr = $('<tr/>');
tr.append("<td>" + sentData.ARTIST[i] + "</td>");
tr.append("<td>" + sentData.TRACKTITLE[i] + "</td>");
tr.append("<td>" + sentData.LOCATION[i] + "</td>");
$('#multiple').append(tr);
}

在这里查看jsfiddle中的工作. http://jsfiddle.net/jspatel/Ya82B/

see the working in jsfiddle here. http://jsfiddle.net/jspatel/Ya82B/

这篇关于JSON编码多个MySQL行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆