1

I have a ajax returning json array data like 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 to show that data by using javascript like 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, I try like 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, I got five line for each user. I was wrong while looping. So, how can I do this?

5
  • 3
    try removing the for loop Commented Feb 8, 2016 at 3:37
  • Thank you so much. Now, Its ok. Commented Feb 8, 2016 at 3:41
  • 2
    @roullie make it as answer Commented Feb 8, 2016 at 3:43
  • @roullie make your comment to answer please. Commented Feb 8, 2016 at 3:55
  • @NightMare comment made to answer. :) Commented Feb 8, 2016 at 4:56

3 Answers 3

1

Try removing the 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);
    });
 //}
Sign up to request clarification or add additional context in comments.

1 Comment

@NightMare no problem. glad i could help. :)
1

Use this code:

$.ajax({
    url: 'some.php',
    type: 'post',
    data: {dept:d},
    dataType: 'json',
    success: function(data) {
        console.log("success");
        var temp = '';
        if(data && data != "") {
            var temp = '<ul>';
            for(var i = 0; i < data.length; i++) {
                temp += '<li>UserName: ' + data[i]['UserName'] + '&nbsp;Total: ' + data[i]['Total'] + '&nbsp;Correct(%): ' + data[i]['Correct(%)'] + '&nbsp;Incorrect(%): ' + data[i]['Incorrect(%)'] + '</li>';
            }
            temp += '</ul>';

            document.getElementById('user').innerHTML = temp;
        }
    }
});

1 Comment

Thank you. Its the different way.
0

You only need to use 1 $.each loop, otherwise you are looping through all the data muliple times.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.