PDA

View Full Version : Problem in encoding jason for tree panel



zeeshanjeelani
28 Feb 2013, 1:08 PM
Hi can anyone suggest how to encode data for jason?
I am getting folder under folder and so on... infinite.





<?php


$parent_id = $_GET['node'];
$query = "SELECT unitobjid, unitname FROM unit ORDER BY unitobjid ASC";



$rs = mysql_query($query);
$i=0;
$arr = array();while($obj = mysql_fetch_array($rs)) {
$arr[] =$obj; $i++; }
echo json_encode($arr);



?>

EPV
28 Feb 2013, 1:24 PM
What do your json look like?

scottmartin
28 Feb 2013, 3:17 PM
You are close .. something like:



$sql = 'SELECT * FROM table';

$result_count = mysql_query($sql);
$rows = mysql_num_rows($result_count); // rows for paging, etc.
$result = mysql_query($sql);

$data = $json->encode($return);

echo '{"total":"' . $rows . '","data":' . $data . '}';

zeeshanjeelani
1 Mar 2013, 1:38 AM
I tried the following code



$parent_id = $_GET['node'];
$sql = "SELECT unitobjid, unitname FROM unit ORDER BY unitobjid ASC"; $result_count = mysql_query($sql);$rows = mysql_num_rows($result_count); // rows for paging, etc.$result = mysql_query($sql); $arr = array();while($obj = mysql_fetch_array($result)) { $arr[] =$obj; } $data = json_encode($arr); echo '({"total":"' . $rows . '","data":' . $data . '})';


And my jason looks like this




({"total":"13","data":[{"0":"1","unitobjid":"1","1":"Pakistan","unitname":"Pakistan"},{"0":"3","unitobjid":"3","1":"Lahore Pakistan","unitname":"Lahore Pakistan"},{"0":"4","unitobjid":"4","1":"Islamabad Pakistan","unitname":"Islamabad Pakistan"},{"0":"5","unitobjid":"5","1":"Faisalabad Pakistan","unitname":"Faisalabad Pakistan"},{"0":"6","unitobjid":"6","1":"Rawalpindi Pakistan","unitname":"Rawalpindi Pakistan"},{"0":"7","unitobjid":"7","1":"Quetta Pakistan","unitname":"Quetta Pakistan"},{"0":"8","unitobjid":"8","1":"Kashmir Pakistan","unitname":"Kashmir Pakistan"},{"0":"9","unitobjid":"9","1":"Sargodha Pakistan","unitname":"Sargodha Pakistan"},{"0":"10","unitobjid":"10","1":"Jhang Pakistan","unitname":"Jhang Pakistan"},{"0":"11","unitobjid":"11","1":"Abbotabad Pakistan","unitname":"Abbotabad Pakistan"},{"0":"12","unitobjid":"12","1":"Gujranwala Pakistan","unitname":"Gujranwala Pakistan"},{"0":"13","unitobjid":"13","1":"Murree Pakistan","unitname":"Murree Pakistan"},{"0":"14","unitobjid":"14","1":"Sialkot Pakistan","unitname":"Sialkot Pakistan"}]})






And still problem is i cannot see any data in tree

scottmartin
1 Mar 2013, 5:26 AM
Remove the () around it ... my fault .. That will validate on jsonlint.

The next step would be to see the model/store config.

Scott.

zeeshanjeelani
1 Mar 2013, 9:00 AM
Thanks for your cooperation.
Still I am unable to see any data in the tree.

My store configs




var store = Ext.create('Ext.data.TreeStore', { proxy: { type: 'ajax', url: 'loadDept.php' }, root: { text: 'Root Node', id: 'root_node', expanded: true }, folderSort: true, sorters: [{ property: 'unitname', direction: 'ASC' }] });
var tree = Ext.create('Ext.tree.Panel', { store: store, renderTo:Ext.getBody(), height: 300, width: 250, allowDrag:true, title: 'ExtJS Tree PHP MySQL', viewConfig: { plugins: { ptype: 'treeviewdragdrop', } } });


The Jason Output, I am getting.





{"total":"13","data":[{"0":"1","unitobjid":"1","1":"Pakistan","unitname":"Pakistan"},{"0":"3","unitobjid":"3","1":"Lahore Pakistan","unitname":"Lahore Pakistan"},{"0":"4","unitobjid":"4","1":"Islamabad Pakistan","unitname":"Islamabad Pakistan"},{"0":"5","unitobjid":"5","1":"Faisalabad Pakistan","unitname":"Faisalabad Pakistan"},{"0":"6","unitobjid":"6","1":"Rawalpindi Pakistan","unitname":"Rawalpindi Pakistan"},{"0":"7","unitobjid":"7","1":"Quetta Pakistan","unitname":"Quetta Pakistan"},{"0":"8","unitobjid":"8","1":"Kashmir Pakistan","unitname":"Kashmir Pakistan"},{"0":"9","unitobjid":"9","1":"Sargodha Pakistan","unitname":"Sargodha Pakistan"},{"0":"10","unitobjid":"10","1":"Jhang Pakistan","unitname":"Jhang Pakistan"},{"0":"11","unitobjid":"11","1":"Abbotabad Pakistan","unitname":"Abbotabad Pakistan"},{"0":"12","unitobjid":"12","1":"Gujranwala Pakistan","unitname":"Gujranwala Pakistan"},{"0":"13","unitobjid":"13","1":"Murree Pakistan","unitname":"Murree Pakistan"},{"0":"14","unitobjid":"14","1":"Sialkot Pakistan","unitname":"Sialkot Pakistan"}]}




and PHP Code



$parent_id = $_GET['node'];
$sql = "SELECT unitobjid, unitname FROM unit ORDER BY unitobjid ASC"; $result_count = mysql_query($sql);$rows = mysql_num_rows($result_count); // rows for paging, etc.$result = mysql_query($sql); $arr = array();while($obj = mysql_fetch_array($result)) { $arr[] =$obj; } $data = json_encode($arr); echo '{"total":"' . $rows . '","data":' . $data . '}';