-
21 Nov 2011 12:28 AM #1
Unanswered: How to get values from database??
Unanswered: How to get values from database??
Hi,
I am just exploring the sencha world.I am creating my first app using sencha. Can someone point me as to how i can get the data from the database using php and json. Ive got a code which i will write down.
Code:function showRecent() { Ext.Ajax.request({ url: 'server.php?action=get', success: function(e) { var obj = Ext.util.JSON.decode(e.responseText); var guests = obj.guests; if (guests) { var html = tpl.applyTemplate(guests); Ext.getCmp('recentTab').update(html); } } }); }Code:if (isset($_GET['action']) && $_GET['action'] == "get") { $sql = "SELECT * FROM messages"; $guests = array(); foreach ($dbh->query($sql) as $row) { $guests[] = array( 'id' => $row['id'], 'name' => $row['name'], 'email' => $row['email'], 'url' => $row['url'], 'message' => $row['message'], 'date' => $row['date_entry'] ); } echo json_encode(array('success' => true, 'guests' => $guests)); }
I am not able to get individual date from the array. Its always saying undefined.
Also can i declare a global variable and use it throughout the project?
I am creating a login page and using that information which i get from login, i need to fill up contents in the app. So i would need the id throughout. Or is it possible to send the value from one panel to another.
Thanks
Roy M J
-
21 Nov 2011 1:22 AM #2Sencha - Services Team
- Join Date
- May 2007
- Location
- Munich (Germany)
- Posts
- 2,292
- Vote Rating
- 6
- Answers
- 58
as a first step: think about which framework you are using:
Sencha Touch 1
ST 2
Ext JS 3
Ext JS 4
for example! then please use the matching forum(s) to ask questions there.
-
21 Nov 2011 1:24 AM #3
-
21 Nov 2011 1:30 AM #4
Hi,
Thanks for replying..
Sorry I am using secha touch 2.
And yes i am setting success as true. But in the js file im not sure how to get values from the array.
The js part is as follows :Code:echo json_encode(array('success' => true, 'guests' => $guests));
Many thanksCode:function showRecent() { Ext.Ajax.request({ url: 'server.php?action=get', success: function(e) { var obj = Ext.util.JSON.decode(e.responseText); var guests = obj.guests; if (guests) { var html = tpl.applyTemplate(guests); Ext.getCmp('recentTab').update(html); } } }); }
Roy
-
21 Nov 2011 1:34 AM #5
What i would like to know is how i can get the individual values from variable guests. Till now i could not figure out how to get the values.
Another thing is once i get the value will it be possible to declare it as a global variable so that i can use that value throughout the app?. Or is it good for me to use php session variablesfor this purpose.
Thanks
Roy
-
21 Nov 2011 1:55 AM #6Sencha - Services Team
- Join Date
- May 2007
- Location
- Munich (Germany)
- Posts
- 2,292
- Vote Rating
- 6
- Answers
- 58
what do you get when logging the response, like:
global vars are a quite bad coding style.Code:success: function(response) { console.log(response.responseText); }
in general, after taking a closer look at your usecase.
when you want to get an array of guests from your server,
a single ajax call is not the best way to go.
did you take a look at:
http://docs.sencha.com/touch/2-0/#!/api/Ext.data.Store
with using a store you can easily access your data, sort, filter, modify etc. it.
-
21 Nov 2011 2:11 AM #7
Hi,
Yes i got the complete list of data from the database.
The problem is i'm new to this and bit hesitant into jumping inside everything as i may loose track of the flow easily. I am in the process of creating a simple login and i just want to store one id in a variable throughout the app. So should i go for store or should i go for normal variable declaration?
I am in the process of learning sencha and im loving it to core. But please bear my questions as my knowledge is limited at this time.
Thanks
Roy M J



Reply With Quote