php - How to show JSON array data by using javascript? -


i have ajax returning json array data this,

[{     "username": "john",     "total": "45",     "correct(%)": "71.1111",     "incorrect(%)": "28.8889" }, {     "username": "kkk",     "total": "42",     "correct(%)": "47.6190",     "incorrect(%)": "52.3810" }, {     "username": "aaa",     "total": "54",     "correct(%)": "81.4815",     "incorrect(%)": "18.5185" }, {     "username": "bbb",     "total": "39",     "correct(%)": "58.9744",     "incorrect(%)": "41.0256" }] 

i want show data using javascript this,

username: john total: 45 correct(%): 71.1111
incorrect(%): 28.8889

username: kkk total: 42 correct(%): 47.6190
incorrect(%): 52.3810

username: aaa total: 54 correct(%): 81.4815
incorrect(%): 18.5185

username: bbb total: 39 correct(%): 58.9744
incorrect(%): 41.0256

so, try this,

$.ajax({                 url: 'some.php',                 type: 'post',                 data: {dept:d},                 datatype: 'json',                 success: function(data) {                     console.log("success");                     var temp = "";                     if(data && data!="") {                         for(var i=0; i<data.length; i++) {                             $.each(data,function(k,v){                                 $.each(v,function(k,s){                                     temp +=k+': <b>'+s+'</b> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';                                 });                                 temp +="<br/><br/>";                                 console.log(temp);                             });                         }                          document.getelementbyid('user').innerhtml= temp;                     } }); 

but, got 5 line each user. wrong while looping. so, how can this?

try removing for loop.

 //for(var i=0; i<data.length; i++) {     $.each(data,function(k,v){         $.each(v,function(k,s){            temp +=k+': <b>'+s+'</b> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';         });         temp +="<br/><br/>";         console.log(temp);     });  //} 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -