View Full Version : How to combine multiple arrays in Json for Form loading

22 Apr 2010, 4:21 AM
Hello Everyone,

I am stuck with this problem, read couple of blogs and also the docs but can't find the solution.

In my php I run two different queries to get the record from the database and then put them in the json array.

If I run the following php code;

Failed to load source for

while($row = pg_fetch_row($res))
$data[] =array(
'Id' =>$row[0],
'Admin1Id' =>$row[2],
'reportType' =>$row[3],
the above code works fine and I can load the form with the values coming through the JSON.

I want to add another query and add the result to the same array $data but then it all messed up and I dont get the values.
please see the code belwo where I add more items to the array

while($staffAff = pg_fetch_row($result3))
$data['staffName'[ =$staffAff[0];
$data['staffContractType'] =$staffAff[1];
$data['nationality'] =$staffAff[2];
$data['organization'] =$staffAff[3];

$result = '{success:true,data:'.$data.'}';
echo result;

this is where I am lost and don't know how to append the values to the array which already has data in the first while loop.

Would really appreciate If someone has gone through the similar experience and got the solution and share with me.


Mike Robinson
22 Apr 2010, 5:52 AM
You really want to build your data structure in-memory and then let the PHP JSON functions prepare for you what is written out to the host-response.

In other words, for the latter part of your task you would create a new structure and append it to the end of an array, which you have already initialized to be part of the total response-packet. Then, after you have built the data structure, json_encode() it and send it out.

The process on the receiving end will be similar. You'll decode the JSON text, back into a (JavaScript) data structure, then check the structure for saneness (behaving graciously if the host hands you "insanity"), then iterate through the structure and populate the various ExtJS data-store objects (manually) with it.

Although AFAIK we do not yet have support for multi-dimensional structures (at least, not in the published code), it does nevertheless serve as a textbook-full of good examples.

22 Apr 2010, 6:26 AM
Would be nice If you can give me a small example or sample that already does something similar you mentioned.