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.
My HTML table on the search page looks something like this:
ARTIST | TITLE | LOCATION
U2 | One | Ireland
Foals | Miami | UK
and the JSON file passed to it utilises similar headings:
{"ARTIST":"Katy Perry","LOCATION":"United States", "TRACKTITLE":"Roar"}
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
extract from PHP file:
$result = mysql_query($sql);
$data = array();
while ($array = mysql_fetch_assoc($result)) {
$data[] = $array;
}
echo json_encode($data);
Edited to include jQuery extract and more specific question:
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);
}
});
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"}]