PDA

View Full Version : (SOLVED) Dynamic Columns showing the right info but underneath eachother



robin30
5 Apr 2010, 3:33 PM
i am having a grid with dynamic columns. it's working. but the records is showing underneath eachother but yet in the right column instead of in the same row.

this is my store:

var infostore = new Ext.data.JsonStore({

url:'database.php',

baseParams:{task:'INFO'},

fields:[]

});


this is my json from store load:

{"columns":[{"header":"id","dataIndex":"id"},{"header":"member_id","dataIndex":"member_id"},{"header":"identCity","dataIndex":"identCity"},{"header":"libelCity","dataIndex":"libelCity"},{"header":"units","dataIndex":"units"}],"records":[{"id":"1"},{"member_id":"3"},{"identCity":"USPA1445"},{"libelCity":"Saxton, PA"},{"units":"english"}],"metaData":{"fields":["id","member_id","identCity","libelCity","units"],"root":"records","total":1,"totalProperty":"total"}}


i do believe it has something to do with the 'records' part. every part is between '{ }' but should be only at the beginning and at the end. or do i see it wrong?

this is my php code:

$result = mysql_query("SHOW FULL COLUMNS FROM `$column`");

$rows = mysql_num_rows($result);

$result1 = "SELECT * FROM `$column`";

$sql = mysql_query($result1);

$rows1 = mysql_num_rows($sql);

$row1 = mysql_fetch_array($sql);

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



$columns[]=array('header'=>$row['Field'],'dataIndex'=>$row['Field']);

$fields2[]= $row['Field'];

$fields[] = array($row['Field']=>$row1[$row['Field']]);

$metadata=array('fields'=>$fields2,'root'=>'records','total'=>$rows1,'totalProperty'=>'total');

$arr=array('columns'=>$columns,'records'=>$fields,'metaData'=>$metadata);

}

$jsonresult = Json_encode($arr);

echo $jsonresult;


any suggestions?

Regards,

robin30

mpawlowski
5 Apr 2010, 3:40 PM
You are right about the records part. I formatted the json below to make it more readable.


{
"columns": [{
"header": "id",
"dataIndex": "id"
}, {
"header": "member_id",
"dataIndex": "member_id"
}, {
"header": "identCity",
"dataIndex": "identCity"
}, {
"header": "libelCity",
"dataIndex": "libelCity"
}, {
"header": "units",
"dataIndex": "units"
}],
"records": [{
"id": "1"
}, {
"member_id": "3"
}, {
"identCity": "USPA1445"
}, {
"libelCity": "Saxton, PA"
}, {
"units": "english"
}],
"metaData": {
"fields": ["id", "member_id", "identCity", "libelCity", "units"],
"root": "records",
"total": 1,
"totalProperty": "total"
}
}

Your PHP code is spitting out an array of records, when what you want it to spit out is something like this:


"records": [{
"id": "1"
"member_id": "3"
"identCity": "USPA1445"
"libelCity": "Saxton, PA"
"units": "english"
}]

Though I'm not a PHP guy so I can't help much there, just confirming your suspicions.

Animal
6 Apr 2010, 2:44 AM
Read the API docs properly. "records" is an Array of RECORDs

robin30
6 Apr 2010, 2:45 AM
thanks for your reply. really appreciate it.

i did put my json output in a text file and removed the brackets.
so it would end up like this.


"records": [{
"id": "1"
"member_id": "3"
"identCity": "USPA1445"
"libelCity": "Saxton, PA"
"units": "english"
}]

and it works. it shows up in 1 row as its suppose to do.

guess i know where the problem is.

but i have no idea how do get it like that in php. i'm not a php guru either,lol
if i figure it out, i will post what i did. maybe it will benefit somebody else.

but thanks for your reply.

regards

Robin30

robin30
6 Apr 2010, 5:26 AM
if i change my php code into this:


$result = mysql_query("SHOW FULL COLUMNS FROM `$column`");
$rows = mysql_num_rows($result);
$result1 = "SELECT * FROM `$column`";
$sql = mysql_query($result1);
$rows1 = mysql_num_rows($sql);
$row1 = mysql_fetch_array($sql);
while ($row = mysql_fetch_array($result)) {
$columns[]=array('header'=>$row['Field'],'dataIndex'=>$row['Field']);
$fields2[]= $row['Field'];
$fields[]=$row['Field'].":".$row1[$row['Field']];
}
$metadata=array('fields'=>$fields2,'root'=>'records','total'=>$rows1,'totalProperty'=>'total');
$arr=array('columns'=>$columns,'records'=>$fields,'metaData'=>$metadata);
$jsonresult = Json_encode($arr);

json output:

{"columns":[{"header":"id","dataIndex":"id"},{"header":"member_id","dataIndex":"member_id"},{"header":"identCity","dataIndex":"identCity"},{"header":"libelCity","dataIndex":"libelCity"},{"header":"units","dataIndex":"units"}],"records":["id:1","member_id:3","identCity:USPA1445","libelCity:Saxton, PA","units:english"],"metaData":{"fields":["id","member_id","identCity","libelCity","units"],"root":"records","total":1,"totalProperty":"total"}}

records is shown right except it's missing the brackets at the beginning and the end.

any suggestions?
regards

Robin30

Condor
6 Apr 2010, 5:50 AM
I know nothing about PHP, but shouldn't that be something like:

$fields[$row['Field']] = $row1[$row['Field']];
or even:

$arr=array('columns'=>$columns,'records'=>$row1,'metaData'=>$metadata);

robin30
6 Apr 2010, 6:14 AM
you're the best condor.

couldn't find the problem.

this is the final result:

$result = mysql_query("SHOW COLUMNS FROM `$column`");
$rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
$columns[]=array('header'=>$row['Field'],'dataIndex'=>$row['Field'],'sortable'=>true);
$fields2[]= $row['Field'];
}
$query = "SELECT * FROM `$column`";
$result1 = mysql_query($query) or die(mysql_error());
$rows1 = mysql_num_rows($result1);
if($rows1 > 0){
while($row1 = mysql_fetch_array($result1)){
$arr1[]= $row1;
}
} else {
$arr1='';
}
$metadata=array('fields'=>$fields2,'root'=>'records','total'=>$rows1,'totalProperty'=>'total');
$arr=array('columns'=>$columns,'records'=>$arr1,'metaData'=>$metadata);
$jsonresult = Json_encode($arr);
echo $jsonresult;

and it works.

thanks so much

regards,

Robin30