本文介绍了我想要一个处理数据库的示例sencha触摸应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的触觉。我需要维护一个数据库(而不是本地存储)为sencha触摸应用程序,任何身体可以帮助我。

解决方案

如果您使用php连接到mysql,这是一个简单的应用程序,我从sencha网站修改。



首先你的javascript

  var helloWorld = new Ext.Application({

launch:function(){

Ext.regModel('Contacts',{
fields:[{
name:'first_last_name'
}]
});

store = new Ext.data.Store({
model:'Contacts',
proxy:{
type:'scripttag ',
url:'contact.php',
reader:{
type:'json',
root:'emergency_contact'
},
extraParams:{
action:'read'
}
}
});



var tmpl = new Ext 。模板(
'< table>',
'< tr>',
'< td>',

'{first_last_name}',

'< / td>',
'< / tr>',
'< / table>'
);

this.tabs = new Ext.TabPanel({
fullscreen:true,
dockedItems:[{
xtype:'toolbar',
title: 'Hello World'
}],
tabBar:{
ui:'light',
layout:{
pack:'center'
}
},
项目:[
{
html:'你好',
标题:'你好'
},

{
html:'world',
title:'world'
},

{
cls:'list',
title :'list',
xtype:'list',
store:store,
itemTpl:tmpl
}
]
});
this.list = this.tabs.items.getAt(2);


store.load();
}

});

然后在商店中声明contact.php

 <?php 
header('Content-Type:text / javascript; charset = UTF-8');

ini_set(display_errors,true);
ini_set(html_errors,true);

包含db_connect.php;

$ out =;
$ action =;
$ data =;
$ contact =;

if(isset($ _ REQUEST [action])){
$ action = $ _REQUEST [action];
}
switch($ action){
caseread:$ out = read_this();
break;
}
echo utf8_encode($ out);

函数read_this(){

$ conn = OpenDbConnection();

$ sql =从联系人中选择first_last_name;



$ result = mysql_query($ sql);

$ num = mysql_numrows($ result);

$ i = 0;

$ eData = array(count=> $ num,contact=> array());

while($ row = mysql_fetch_assoc($ result)){
$ eData [contact] [$ i] = $ row;
$ i ++;
}

CloseDbConnection($ conn);
// return json_encode($ eData);
返回$ _REQUEST ['callback']。 '('。json_encode($ eData)。');';
}

?>

然后使用PHP连接到数据库(db_connect)

 <?php 
function OpenDbConnection(){


$ dbhost ='localhost:3306';
$ dbuser ='phpapp';
$ dbpass ='phpapp';
$ dbname ='dbname';



$ conn = mysql_connect($ dbhost,$ dbuser,$ dbpass);

如果(!$ conn){
echo(无法连接到数据库服务器);
exit();
}

mysql_select_db($ dbname)or die(选择数据库时出错);

return $ conn;
}

函数CloseDbConnection($ conn){
mysql_close($ conn);
}
?>

这是一个sencha touch 1应用程序,但可以轻松修改以使用sencha touch 2.希望有助于


I am new to sencha touch. I have a requirement for maintaining a database (not local storage)for the sencha touch application, can any body help me.

解决方案

If your using php to connect to mysql this is a simple app I modified from the sencha site.

First your javascript

var helloWorld = new Ext.Application({

    launch: function() {

        Ext.regModel('Contacts', {
            fields:[{
                name:'first_last_name'
            }]
        });

        store = new Ext.data.Store({
            model: 'Contacts',
            proxy: {
                type: 'scripttag',
                url: 'contact.php',
                reader: {
                    type: 'json',
                    root: 'emergency_contact'
                },
                extraParams:{
                    action:'read'
                }
            }
        });



        var tmpl = new Ext.Template(
            '<table>',
            '<tr>',
            '<td>',

            '{first_last_name}',

            '</td>',
            '</tr>',
            '</table>'
            );

        this.tabs = new Ext.TabPanel({
            fullscreen: true,
            dockedItems: [{
                xtype:'toolbar', 
                title:'Hello World'
            }],
            tabBar: {
                ui: 'light',
                layout: {
                    pack: 'center'
                }
            },
            items: [
            {
                html:'Hello',
                title:'Hello'
            },

            {
                html:'world', 
                title:'world'
            },

            {
                cls: 'list',
                title: 'list',
                xtype: 'list',
                store: store,
                itemTpl:tmpl
            }
            ]
        });
        this.list = this.tabs.items.getAt(2);


        store.load();
    }

});

And then contact.php declared in the store

<?php
header('Content-Type: text/javascript; charset=UTF-8');

ini_set("display_errors", true);
ini_set("html_errors", true);

include "db_connect.php";

$out = "";
$action = "";
$data = "";
$contact = "";

if (isset($_REQUEST["action"])) {
    $action = $_REQUEST["action"];
}
switch ($action) {
    case "read": $out = read_this();
        break;
}
echo utf8_encode($out);

function read_this() {

    $conn = OpenDbConnection();

    $sql = "select first_last_name from contact";



     $result = mysql_query($sql);

    $num = mysql_numrows($result);

    $i = 0;

    $eData = array("count" => $num, "contact" => array());

    while ($row = mysql_fetch_assoc($result)) {
        $eData["contact"][$i] = $row;
        $i++;
    }

    CloseDbConnection($conn);
  //  return json_encode($eData);
    return $_REQUEST['callback'] . '(' . json_encode($eData) . ');';
}

?>

And then connecting to the database (db_connect) with php

<?php
function OpenDbConnection() {


  $dbhost = 'localhost:3306';
  $dbuser = 'phpapp';
  $dbpass = 'phpapp';
  $dbname = 'dbname';



  $conn = mysql_connect($dbhost, $dbuser, $dbpass);

  if (!$conn) {
    echo( "Unable to connect to the database server." );
    exit();
  }

  mysql_select_db($dbname) or die( "Error selecting database.");

  return $conn;
}

function CloseDbConnection($conn) {
  mysql_close($conn);
}
?>

This a sencha touch 1 app but can easily be modified to work with sencha touch 2. Hope that helps

这篇关于我想要一个处理数据库的示例sencha触摸应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 14:26