PDA

View Full Version : Load data from MySQL and make a list of it



oskars
28 Jun 2010, 8:16 AM
Hello!

I've been looking for a way to load data from a MySQL database, but I can't find a good example of how to do it.

Is the best way to use AJAX?

Would be very happy if someone can show me a example!

Best regards
Oskar, Sweden

solodesignz
28 Jun 2010, 10:06 AM
Yeah, use AJAX to load a server-side file (php for example). Then in that file you can grab all the data you want and return it as JSON or XML, then parse it with the JS.

If that's what you were asking =)

oskars
28 Jun 2010, 10:40 AM
Yes! That is what I am asking for...

I now know how to load the server-side file (ASP)... but how do I return it as JSON or XML (which is best?)...

My server-side file format is like this:
Name#Lastname#Phonenumber
Name#Lastname#Phonenumber
Name#Lastname#Phonenumber

I would like to make a list of it... =)

solodesignz
28 Jun 2010, 10:47 AM
Well, i havnt tried anything in sencha touch yet... but a store in regular EXTJS would be like this:



this.store = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url: 'URL_TO_SERVER_FILE'
}),
reader: new Ext.data.JsonReader({
root: 'items',
totalProperty: 'totalCount',
id: 'Id'
}, [
{name: 'Id'},
{name: 'Name'},
{name: 'Lastname'},
{name: 'Phonenumber'}
])
});


Then the serverside JSON from ASP would spit out:



{
"totalCount":"2",
"items":[{
"Name":"John Doe",
"Lastname":"Doe",
"Phonenumber":"555-555-5555",
"Id":1
},{
"Name":"Jane Doe",
"Lastname":"Doe",
"Phonenumber":"555-555-5555",
"Id":2
}]
}


Then you can apply that store to anything... ie: grids

Hopefully sencha touch operates in that same way, someone correct me if I'm way off =)

solodesignz
28 Jun 2010, 10:50 AM
Hmm.. or are you asking how to basically read the DB and loop through records to create the JSON in ASP...

oskars
28 Jun 2010, 11:01 AM
Thanks for your help!

Hm... I don't know... Maybe it is easier to let ASP create the JSON-file?

solodesignz
28 Jun 2010, 11:04 AM
Yeah, anything coming from a DB will have to be created via ASP... and passed as JSON.
Then Sencha will parse that JSON file...

oskars
28 Jun 2010, 11:21 AM
So what I have to do is to make my ASP loop out my database records like this:




{
"totalCount":"2",
"items":[{
"Name":"John Doe",
"Lastname":"Doe",
"Phonenumber":"555-555-5555",
"Id":1
},{
"Name":"Jane Doe",
"Lastname":"Doe",
"Phonenumber":"555-555-5555",
"Id":2
}]
}




And when I have that file (ex. ajax.asp?json=contacts), then I can somehow parse it in javascript?

solodesignz
28 Jun 2010, 11:24 AM
Exactly...

you'll read that ASP file with a store.. then apply that store to anything... grid, list, combobox... etc

nguyenngoctoan
25 Aug 2010, 11:00 AM
Can anyone post a full code example where data is quried from MySQL db via php then passed to JSON used in Sencha Touch?

Thank you in advance.

evant
25 Aug 2010, 6:12 PM
This isn't a PHP forum, so you're more likely to have better luck trying somewhere else.

Again, Touch can receive data from any platform, as long as it's formatted as JSON (or XML...).

tuamo
22 Sep 2010, 12:54 PM
I need the same tiny please with sugar on top someone could give an example

Theodosis
22 Sep 2010, 2:42 PM
<?php
mysql_connect( 'localhost', 'user', 'pass' );
mysql_select_db( 'db' );

$res = mysql_query( 'SELECT * FROM `table`;' );

while( $row = mysql_fetch_array( $res ) ){
$rows[] = array(
'Name'=> $row[ 'Name' ],
'Lastname' => $row[ 'Lastname' ],
'Phonenumber' => $row[ 'Phonenumber' ],
'Id' => $row[ 'Id' ]
);
}



$return = array(
'totalCount' => count( $rows ),
'items' => $rows
);
echo json_encode( $return );
?>



assuming that you have a table named 'table' with the fields Name, Lastname, Phonenumber, Id, that code should build the json you need

tuamo
23 Sep 2010, 10:08 AM
Thank you very much for that piece of code .. was not far wrong, my failure is that in ext. I do not return anything. if I can lend a hand would be eternally grateful .. this is my source code and can view in www.fonotecaexperimental.es/ipad2 in menu user interface / list


Ext.regModel('Contact', {
fields: ['titulo']
});

demos.ListStore = new Ext.data.Store({
model: 'Contact',
sorters: 'titulo',
getGroupString : function(record) { return record.get('data')[0]; },
proxy: new Ext.data.HttpProxy({url: 'consulta.php' }),
reader: new Ext.data.JsonReader({ root: 'data' },
[
{titulo: 'titulo'}
]),

});


demos.List = new Ext.Panel({
layout: Ext.is.Phone ? 'fit' : {
type: 'vbox',
align: 'center',
pack: 'center'
},
cls: 'demo-list',
items: [{
width: Ext.is.Phone ? undefined : 300,
height: 500,
xtype: 'list',
disclosure: {
scope: demos.ListStore,
handler: function(record, btn, index) {
alert('Disclose more info for ' + record.get('titulo'));
}
},
store: demos.ListStore,
tpl: '<tpl for="."><div class="contact"><strong>{titulo}</strong></div></tpl>',
itemSelector: 'div.contact',
singleSelect: true,
grouped: true,
indexBar: true
}]
});

consultas.php contains similar code theodosis only that connecting to mysql server

Update: I just update the source code, but does not work. could someone guide me where I'm wrong?