如何将sencha touch应用程序与mysql数据库连接到PHP后端?
我知道这个问题可能在其他地方得到了解答,但我仍然被困在那里,我已经查了很多资料来源。我已经有了sencha列表视图、模型和存储设置,以及一个带有PHP+json转换的mysql数据库。但它仍然不会将结果显示到我的列表视图中。我的sencha应用程序运行时没有任何错误。经过大量的研究,我发现在我的store类中定义根属性肯定是做错了什么。以下是我的PHP+json代码,其中包含从数据库获取数据的mysql查询:如何将sencha touch应用程序与mysql数据库连接到PHP后端?,php,mysql,json,sencha-touch,Php,Mysql,Json,Sencha Touch,我知道这个问题可能在其他地方得到了解答,但我仍然被困在那里,我已经查了很多资料来源。我已经有了sencha列表视图、模型和存储设置,以及一个带有PHP+json转换的mysql数据库。但它仍然不会将结果显示到我的列表视图中。我的sencha应用程序运行时没有任何错误。经过大量的研究,我发现在我的store类中定义根属性肯定是做错了什么。以下是我的PHP+json代码,其中包含从数据库获取数据的mysql查询: <?php header('Content-Type: text/javascr
<?php
header('Content-Type: text/javascript; charset=UTF-8');
ini_set("display_errors", true);
ini_set("html_errors", true);
//checking connection and connecting to a database
require_once('connection/config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//variables initialization
$out = "";
$action = "";
$data = "";
//check if read is set from the Teams class
if (isset($_REQUEST["action"])){
$action = $_REQUEST["action"];
}
switch ($action) {
case "read": $out = read_this();
break;
}
echo utf8_encode($out);
//using function read_this() to retrieve teams from the tb_teams table then convert into json data
function read_this(){
$result=mysql_query("SELECT home_team_name FROM tb_teams")
or die("There are no records to display ... \n" . mysql_error());
$num = mysql_num_rows($result);
$i = 0;
$eData = array("count" => $num, "fixtures" => array());
while ($row = mysql_fetch_assoc($result)){
$eData["fixtures"][$i] = $row;
$i++;
}
return $_REQUEST['callback'] . '(' . json_encode($eData) . ');';
}
?>
我的店铺类别:
Ext.define('App.store.Teams',{
extend: 'Ext.data.Store',
config: {
model: 'App.model.Team',
sorters: 'home_team_name',
grouper: function(record) {
return record.get('home_team_name')[0];
},
proxy: {
type: 'scripttag',
url: 'http://127.0.0.1/backend/store.php',
reader: {
type: 'json',
root: 'fixtures'
},
extraParams: {
action: 'read'
}
}
}
});
和我的列表视图类:
Ext.define('App.view.TeamList',{
extend: 'Ext.List',
xtype: 'teamlist',
config: {
title: 'Teams',
//loading data from Teams store into a List item and apply some properties
grouped: true,
indexBar: true,
itemTpl: '{ home_team_name }',
store: 'Teams',
}
});
请有人告诉我到底哪里做错了?提前感谢。假设您得到的是正确的json表单服务器;有时,在列表中添加高度和宽度可能会有所帮助
width: "95%",
height: "95%",
我没有足够的声誉去评论 我认为您应该使用
itemTpl:'{name}'
来代替itemTpl:'{home\u team\u name}'
。因为您在模型中将name
声明为字段
Ext.define('App.view.TeamList',{
extend: 'Ext.List',
xtype: 'teamlist',
config: {
title: 'Teams',
//loading data from Teams store into a List item and apply some properties
grouped: true,
indexBar: true,
itemTpl: '{ name}',
store: 'Teams',
}
});
经过大量观察,我发现问题实际上在于PHP到json的转换(改编自sencha touch文档)以及我如何处理store类中的回调 一个有效的PHP代码:
<?php
header('Content-Type: text/javascript; charset=UTF-8');
ini_set("display_errors", true);
ini_set("html_errors", true);
//checking connection and connecting to a database
require_once('connection/config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//variables initialization
$out = "";
$action = "";
//check if read is set from the Teams class
if (isset($_REQUEST["action"])){
$action = $_REQUEST["action"];
}
switch ($action) {
case "read": $out = read_this();
break;
}
echo utf8_encode($out);
//using function read_this() to retrieve teams from the tb_teams table then convert into json data
function read_this(){
$result=mysql_query("SELECT home_team_name FROM tb_teams")
or die("There are no records to display ... \n" . mysql_error());
$arr = array();
while($obj = mysql_fetch_object($result)) {
$arr[] = $obj;
}
$callback = $_REQUEST['callback'];
if ($callback) {
header('Content-Type: text/javascript');
return $callback . '(' . json_encode($arr) . ');';
} else {
header('Content-Type: application/x-json');
return json_encode($arr);
}
}
?>
再次感谢所有试图帮助的人。我希望这能为其他队友节省大量时间。谢谢您的反馈。我试过了,只是缩小了列表视图的宽度和高度。仍然没有任何东西出现在视图中,我也没有看到任何人真正提供帮助。问题是代码的某些部分我不是很熟悉,比如store类中的root属性及其与PHP或mysql的关系。感谢您的反馈sagar。不管你有多高的声誉,重要的是你花了一些时间去帮助一个伴侣。我试过你的建议,但没有成功。主要是因为“name”实际上不是字段,而是用于声明字段的关键字。再次感谢你的想法。你好。您收到的数据正确吗?PHP以JSON格式发送正确的数据。你提出这样的要求了吗?商店里有人吗?您可以在没有回调的情况下发布从PHP发送的数据吗?
<?php
header('Content-Type: text/javascript; charset=UTF-8');
ini_set("display_errors", true);
ini_set("html_errors", true);
//checking connection and connecting to a database
require_once('connection/config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//variables initialization
$out = "";
$action = "";
//check if read is set from the Teams class
if (isset($_REQUEST["action"])){
$action = $_REQUEST["action"];
}
switch ($action) {
case "read": $out = read_this();
break;
}
echo utf8_encode($out);
//using function read_this() to retrieve teams from the tb_teams table then convert into json data
function read_this(){
$result=mysql_query("SELECT home_team_name FROM tb_teams")
or die("There are no records to display ... \n" . mysql_error());
$arr = array();
while($obj = mysql_fetch_object($result)) {
$arr[] = $obj;
}
$callback = $_REQUEST['callback'];
if ($callback) {
header('Content-Type: text/javascript');
return $callback . '(' . json_encode($arr) . ');';
} else {
header('Content-Type: application/x-json');
return json_encode($arr);
}
}
?>
Ext.define('App.store.Teams',{
extend: 'Ext.data.Store',
config: {
model: 'App.model.Team',
sorters: 'home_team_name',
grouper: function(record) {
return record.get('home_team_name')[0];
},
proxy: {
type: 'scripttag',
url: 'http://127.0.0.1/backend/store.php',
reader: {
type: 'json',
root: 'fixtures'
},
extraParams: {
action: 'read'
}
}
autoLoad true,
}
});