-1

this is my code on server side

$results = array();
while($rw = $objS->row($rs))
 {
$results[]=array('id'=>$rw["id"],
'agent_id'=>$rw["agent_id"],
'agent'=>$rw["agent"]);
}
echo json_encode($results);

on client side i have this coding

success: function( data ) {
var forum = data.results;
for(i = 0, l = forum.length; i < l; i++) {
row = forum[i];
alert(row.id);
}

how to develop json on server side? my json returns is

[{"id":"1","agent_id":"1","agent":"Rustomadmin"}]

i need like this

{"results":[{"id":1,"agent_id":"888","agent":"Emili"}]}
4
  • You need it like that because your client-side code accesses data.results. Maybe you can just write var forum = data; instead, or simply use data directly? Commented Apr 13, 2013 at 9:29
  • alert(data) give me [object Object],[object Object] Commented Apr 13, 2013 at 9:38
  • Yes, it is an array of two objects, the same thing data.results would have given you if your server-side code generated a results object. Try using data directly instead of forum in your loop. Commented Apr 13, 2013 at 9:40
  • @user2244804 : You said, the problem only from server side, then whats the reason to discuss on client side script.. Is there also any problem? Commented Apr 13, 2013 at 9:46

1 Answer 1

1

Your array() declaration is totally wrong.. Try this..

To populate multiple record values means,

<?php
    while($rw = $objS->row($rs))
     {
       $results[] = $rw;         // It generate Array of Array
     }

   $agentResult['results']  = $results;

   $jsonResult = json_encode($agentResult);
  echo $jsonResult;
?>
Sign up to request clarification or add additional context in comments.

2 Comments

sir this is also not working...sir how to create it exactly like this...{"results":[{"id":1,"agent_id":"888","agent":"Emili"}]}
this is good,but i want to add another query in while loop and get some value and want to pass in result array also..

Your Answer

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