Skip to content Skip to sidebar Skip to footer

Iterating And Displaying Json Data With Jquery

I have to display a set of records in a html table. I'm using PHP and jQuery for this. This is my result set which is retrieved using json_encode() This is the output of beta.php [

Solution 1:

$(document).ready(function(){
    $.getJSON('beta.php' , function(data){
        $.each(data, function(){
            $("<table/>").appendTo("document body")
                .html("<tr><td>" + this.StuId + "</td><td>" +  this.fName + "</td><td>" + this.lName + "</td><td>" + this.age +  "</td><td>" + this.grade + "</td></tr>");
        });
    });
});

In your first example, you appended a new table to the document, and then content to another new table. In your second and third try, you set the content of the first table element present in your document.

Edit: And you iterated too deep, one iteration should suffice.

Solution 2:

Try with

  $.getJSON("beta.php", function(data) {
    var table = $("<table>").appendTo(document.body);
    $.each(data, function(i, row) {
      var tr = $("<tr>").appendTo(table);
        $("<td>").appendTo(tr).html(row.StuId);
        $("<td>").appendTo(tr).html(row.fName);
        $("<td>").appendTo(tr).html(row.lName);
        $("<td>").appendTo(tr).html(row.age);
    });
  });

Cheers

Post a Comment for "Iterating And Displaying Json Data With Jquery"