0

I have encoded data in json format by php using the following code

  <?php

    $response = array();

    while ($row = mysql_fetch_array($result)) {

            $user["id"]                 = $row["id"];
            $user["name"]               = ucfirst($row["user_name"]);
            $user["date"]               = $row["date_of_treatment"];
            $user["age"]                = $row["age_of_user"];

            // push single user into final response array
            array_push($response, $user);

            $count = $count+1;
            $sum_of_age = $sum_of_age+$row["age_of_user"];

        }

    $response["average_age"]  = $sum_of_age / $count;
    $response["count"] = $count;

    echo json_encode($response);

?>

I have to decode this json in jquery for that the i have used this method

success: function(result){  

      if(result.length > 0) {      
          for(var i=0; i < result.length; i++) {
            obj = result[i];

            output = output + "<tr><td>"+(i+1)+"</td><td>"+obj.name+"</td><td>"+obj.age+"</td><td>"+obj.date+"</td><tr>";
          } 
          output = output+"<tr><td colspan='2' style='text-align:center'>"+obj.average_age+"</td></tr>"
        } else {
          output = output + "<tr><td colspan='4' style='text-align:center'>No Records Found..!</td></tr>";
        }
         $("#search-list tbody").html(output);     
      }  
  });

but this is not working. please help me to get this correct

result is getting in console in this format. how to iterate through this ?.

{"0":{"id":"35","name":"Ahamed shajeer","date":"2014-03-03","age":"25"},"1":{"id":"36","name":"Meshajeer","date":"0000-00-00","age":"25"},"2":{"id":"37","name":"Iam shajeer","date":"0000-00-00","age":"25"},"average_age":25,"count":3}
3
  • Please explain "not working". Commented Mar 22, 2014 at 14:47
  • @LShetty the response is getting in console, but cant manipulate, here the result.length shows undefined Commented Mar 22, 2014 at 14:52
  • try my answer. let me know so I can assist you. Commented Mar 22, 2014 at 17:01

4 Answers 4

0
success: function(result){  
         var users = result.users;

if(users.length > 0) { 

      for(var i=0; i < users.length ; i++) {
        obj = users[i];

        output = output + "<tr><td>"+(i+1)+"</td><td>"+obj.name+"</td><td>"+obj.age+"</td><td>"+obj.date+"</td><tr>";
      } 
      output = output+"<tr><td colspan='2' style='text-align:center'>"+result.average_age+"</td></tr>"
    } else {
      output = output + "<tr><td colspan='4' style='text-align:center'>No Records Found..!</td></tr>";
    }


       $("#search-list tbody").html(output);     
      }  
  });
Sign up to request clarification or add additional context in comments.

2 Comments

shows an error "ReferenceError: users is not defined"
@ shajeer ,I have edited the answer to your previous question, do as answer is there and you can use recently posted answer(by me) here for your solution.
0

Because your array is not single array. it's an associative array(kind of dictionary).

what you did is you assign array element by index to get user info but how about $response["average_age"] and $response["count"]? how can you get name,age,date info from this?

you should have to pass two array one for users objects like $users and another for your rest response.

2 Comments

how can i retrieve values then ?
make a new array called $users = new Array(); then push your each user object into it. and finally after loop complete then assign $users array like $response['users'] = $users and in jquery check result.users.length it gives length of your user objects and get info from it and for average_age get it result.average_age and same as for count.
0

So, first of all, you need to convert JSON to an array to be able to get the length. So, try:

success: function(result){
    var resultArr = $.parseJSON(result);
    alert (resultArr.length); //3
    //proceed with for loop here
    ....

PHP

$response["average_age"]  = $sum_of_age / $count;
$response["count"] = $count;
header('Content-type: application/json');
echo json_encode($response);

1 Comment

shows error "SyntaxError: JSON.parse: unexpected character"
0

try this

success: function(data) {
    var output = "";
    if (data.length > 0) {
        $.each(data, function(index, item) {
            output += "<tr><td>" + (i + 1) + "</td><td>" + item.name + "</td><td>" + item.age + "</td><td>" + item.date + "</td><tr>";
            output += "<tr><td colspan='2' style='text-align:center'>" + item.average_age + "</td></tr>";
        });
    } else {
        output += "<tr><td colspan='4' style='text-align:center'>No Records Found..!</td></tr>";
    }
    $('#search-list tbody').html(output);
}

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.