-
20 Sep 2011 9:45 PM #1
returning json_encode
returning json_encode
Hey everyone I was hoping someone could help solve a problem where I'm trying to return a query using json_encode. I'm using the MVC tutorial on the site and trying to use the direct classes. my query looks like this
and my store looks likeCode:$rows = array(); while ($row = mysql_fetch_assoc($result)) { $rows[] = $row; } $data = json_encode($rows); return array( 'data' => $data );
and my modelCode:Ext.define('Application.store.Scorecards',{ extend: 'Ext.data.Store', model: 'Application.model.Scorecard', autoLoad: true, proxy: { type: 'direct', directFn: Ext.emptyFn, reader: { type: 'json', root: 'data', } } });
when I run the app I just get a grid with a bunch of empty rows and my router class returns thisCode:// JavaScript Document Ext.define('Application.model.Scorecard' ,{ extend: 'Ext.data.Model', fields: ['Id','Name','Owners'], validations: [{ type: 'length', field: 'Name', min: 1 }, { type: 'length', field: 'Owners', min: 1 }], });
[{"type":"rpc","tid":1,"action":"Scorecard","method":"getScorecards","result":{"data":"[{\"Id\":\"1\",\"Name\":\"First Scorecard\",\"Owners\":\"Me\"}]"}},{"type":"rpc","tid":2,"action":"Scorecard","method":"getScorecards","result":{"data":"[{\"Id\":\"1\",\"Name\":\"First Scorecard\",\"Owners\":\"Me\"}]"}}]
-
21 Sep 2011 4:00 PM #2
Tweaks
Tweaks
So I'm working on this more now and I notice the JSON object my router returns is
"{"Id":"1","Name":"First Scorecard","Owners":"Me"}"
Thats the entry that is in the 'data' array that comes back.
However I noticed some tutorials the JSON returned inside the array looks like
Object {id=1, name="John Doe", email="john@doe.com", ...}
Instead of a bunch of blank rows I'm now getting the correct number of rows, 1 in this case, but it's still blank.
-
21 Sep 2011 6:35 PM #3
Workaround for now....or is this what everyone else does?
Workaround for now....or is this what everyone else does?
so instead of trying to use Json_encode and having no luck I just changed my query to this.
So its working now I just feel like there's a better way to do it. If anyone has any improvements and ideas feel free to share.Code:$rows = array(); while ($row = mysql_fetch_assoc($result)) { $rows[] = array('Id' => $row["Id"], 'Name' => $row["Name"], 'Owners' => $row["Owners"]); } return array( 'data' => $rows, 'success' => TRUE );
-
29 Sep 2011 4:06 PM #4
I think that should work:
havent't programmed in php for quite some time.Code:$rows = array(); while ($row = mysql_fetch_assoc($result)) { $rows[] = $row; } return array( 'data' => $rows, 'success' => TRUE );


Reply With Quote