[英]extjs 4 : filling combobox using MYSQL db via PHP script
Hello i'm trying to populate a combobox with data coming from MYSQL database. 您好,我正在尝试使用来自MYSQL数据库的数据填充组合框。 Using Extjs 4 with the MVC architecture. 在MVC体系结构中使用Extjs 4。
Here's the combobox configuration within my view : 这是我视图中的组合框配置:
...
{
xtype: 'combobox',
id: 'cmbMetric',
name: 'metr',
mode: 'queryMode',
querymode : 'lcoal',
fieldLabel: 'Metric',
store: 'MetricsData',
editable: false,
valign : 'middle',
margin : 15
}
....
My store : 我的商店:
Ext.define('Metrics.store.MetricsData', {
extend: 'Ext.data.Store',
model: 'Metrics.model.MetricsData',
autoLoad: true,
proxy : {
type : 'ajax',
actionMethods : 'POST',
api : {
read : 'testmysql.php'
},
reader: {
type: 'json',
successProperty: 'success',
messageProperty: 'message',
root: 'data'
}
}
});
My model : 我的模特:
Ext.define('Metrics.model.MetricsData', {
extend: 'Ext.data.Model',
fields: [{name : 'name_metric'}]
});
Finally my PHP script : 最后是我的PHP脚本:
<?php
//database parameters
$user='user';
$pw='';
$db='mydb';
$table='metric';
//make database connection
$connection = mysql_connect("localhost", $user, $pw) or
die("Could not connect: " . mysql_error());
mysql_select_db($db) or die("Could not select database");
metricsName();
function metricsName()
{
$sql = 'SELECT name_metric FROM metric';
$result = mysql_query($sql); // result set
while($rec = mysql_fetch_array($result, MYSQL_ASSOC)){
$arr[] = $rec;
};
$data = json_encode($arr); //encode the data in json format
}
?>
I don't know what's wrong or where the error is coming from, but my combobox is never filled. 我不知道出了什么问题或错误是从哪里来的,但是我的组合框从未装满。 Any help would be much appreciated. 任何帮助将非常感激。
You need to return echo
the $data
at the end. 您需要在最后返回echo
$data
。
$data = json_encode($arr); //encode the data in json format
echo $data;
Also your returned data should look something like this: 同样,您返回的数据应如下所示:
{
data: [{
name_metric: 'record1'
}, {
name_metric: 'record2'
}],
total: 2,
success: true
}
Two things: 两件事情:
{ {\n "success": true, “成功”:是的,\n "message": "OK", “ message”:“确定”,\n "data": [ “数据”:[\n { {\n "id": 1, “ id”:1\n "name_metric": "Value1" “ name_metric”:“ Value1”\n }, },\n { {\n "id": 2, “ id”:2\n "name_metric": "Value2" “ name_metric”:“ Value2”\n } }\n ] ]\n} }That means that your function should return just the data array, rather than the encoded one, and php should output should go something like this: 这意味着您的函数应该只返回数据数组,而不是编码数组,而php应该输出如下内容:
echo json_encode(array( 回声json_encode(array(\n 'success' => true, '成功'=>是的,\n 'message' => "OK", 'message'=>“确定”,\n 'data' => metricsName() '数据'=> metricsName()\n)); ));
{ {\n xtype: 'combobox', xtype:'combobox',\n store: Ext.create("Metrics.store.MetricsData"), 商店:Ext.create(“ Metrics.store.MetricsData”),\n displayField: 'name_metric', displayField:'name_metric',\n valueField: 'id' valueField:'id'\n} }\n\n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.