PDA

View Full Version : data store wont add data transmitted via PHP



ONeill
17 Jun 2012, 8:30 AM
Hello guys,

i dispair at my problem :( I think its only a simple bug i have created in the code, but i don't find them :(
The Problem:
I have a data store:


Ext.create('Ext.data.Store', { id:'romStoreid', storeId:'romStore', fields:['id', 'name'], data:{'items':[ <?php $sql = mysql_query('SELECT id, name, lasttime, device FROM content WHERE access = "1"'); if (mysql_num_rows($sql) == 0) { echo 'No resources found.<br>'; } else { while ($c = mysql_fetch_array($sql)) { $device = mysql_query('SELECT deviceName FROM devices WHERE deviceID = "'.mysql_real_escape_string($c["device"]).'"'); $device = mysql_fetch_array($device); echo "{ 'id': '".$c[id]."', \"name\":\"".$device[deviceName]." ".$c[name]."\" },"; } } ?> ]}, proxy: { type: 'memory', reader: { type: 'json', root: 'items' } } });
and the data store is used for my grid:


{ id: 'testernavpanel', title: 'Available Roms', items: [{ xtype: 'grid', title: '', id: 'romGrid', store: Ext.data.StoreManager.lookup('romStore'), columns: [ { header: 'ID', dataIndex: 'id', hidden: true, hideable: false}, { header: 'Name', dataIndex: 'name', width: 240 } ], width: 250, renderTo: Ext.getCmp('testernavpanel'), }], iconCls: 'info' }
Now i have a refresh button. The task of this button is to delete all values in the store and load them new from another php file using ajax:


Ext.Ajax.request({ url: 'expand.php?view=testers_new', method: 'GET', callback: function(options, success, response) { var data = response.responseText; a.getStore().removeAll(); a.getStore().add(data); console.log(data); } });

The new data come from this php code:


$sql = mysql_query('SELECT id, name, device FROM content WHERE access = "1"'); if (mysql_num_rows($sql) == 0) { echo 'No resources found.<br>'; } else { while ($c = mysql_fetch_array($sql)) { $device = mysql_query('SELECT deviceName FROM devices WHERE deviceID = "'.mysql_real_escape_string($c["device"]).'"'); $device = mysql_fetch_array($device); echo "{id: '".$c[id]."', name: '".$device[deviceName]." ".$c[name]."'}, "; } echo "{id:'', name:\"\"}"; }
Now the big problem is, that after i clicked on refresh, all values will be removed, ok. But then only one empty value is added.
The json encoded data is correct transmitted from the php file, i have copied the data from the console and added them to the "a.getStore().add(data);" instead of "data". Then all is ok, and the new data is inserted as the new values.

Has anyone of you an idea why the data of the php file inserted in the store?
Thanks for help and if any more code is needed, please ask ;)

Greetings

ONeill
18 Jun 2012, 12:20 PM
Hello,

i have test myself and the following, not working code:
The Store:


var romStore = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url:'expand.php?view=testers_new',
method: 'GET'
}),
fields: ['id', 'name'],
reader: new Ext.data.JsonReader({
id: 'JSON_id',
totalProperty: 'totalCount',
root: "data",
fields:
[{name: 'id', type: 'int', mapping: 'id'},
{name: 'name', type: 'string', mapping: 'name'}
]
}),
});

romStore.load();


The PHP Code:

$sql = mysql_query('SELECT id, name, device FROM content WHERE access = "1"'); if (mysql_num_rows($sql) == 0) {
echo 'No resources found.<br>';
}
else {
$i = 1;
while ($c = mysql_fetch_array($sql)) {
$device = mysql_query('SELECT deviceName FROM devices WHERE deviceID = "'.mysql_real_escape_string($c["device"]).'"');
$device = mysql_fetch_array($device);
//echo "{id: '".$c[id]."', name: '".$device[deviceName]." ".$c[name]."'}, ";
$data[] = array(
'id' => $c['id'],
'name' => $device['deviceName'].' '.$c['name'],
);
}
$count = mysql_num_rows($sql);
$myData = array('data' => $data, 'totalCount' => $count);
echo json_encode($myData);
}
An example JSON string:


{"data":[{"id":"7","name":"HTC Wildfire S .sense"},{"id":"8","name":"HTC Wildfire S Auto-Installers GUI"},{"id":"21","name":"HTC Wildfire S MIUI Port"},{"id":"22","name":"HTC Wildfire S CM7 Nighlys"},{"id":"24","name":"HTC Wildfire S MIUI v4"},{"id":"25","name":"HTC Wildfire S CM9 for Marvel"},{"id":"44","name":"HTC Wildfire S bgnb g"}],"totalCount":7}


I hope, that somebody can help, idk what i can do now :(

carol.ext
19 Jun 2012, 5:58 AM
I could not read your first post. Based on your second post, did you try putting the reader on your proxy instead of on your store?

ONeill
19 Jun 2012, 7:52 AM
Hmm, good idea. But then i must load the extjs framework in the php file, or?

ONeill
19 Jun 2012, 9:24 AM
Sorry for the silly last post from me ;) I was online with my smartphone and i think i haven't read the help post really good :(

I have moved the reader to the proxy and now the store updates! :) THANKS FOR YOUR VERY GOOD HELP! I was so silly to not see this problem :(