简体   繁体   English

extjs 4:通过PHP脚本使用MYSQL db填充组合框

[英]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: 两件事情:

  • Ext seems to like it when you define an id property/field, it defaults to id. 当您定义id属性/字段时,Ext似乎很喜欢它,它默认为id。 You should add the primary key to your sql query and return it in the data either aliased to or named "id", or change the idProperty of the proxy to match the name you send. 您应该将主键添加到sql查询中,并在别名为“ id”或名为“ id”的数据中返回主键,或者更改代理的idProperty以匹配您发送的名称。
  • ExtJS expects your json to be in a format something like this given how you have that proxy defined ExtJS希望您的json的格式类似于给定的代理定义方式
     { {\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)); )); 
  • Finally, the combo box definition is off. 最后,组合框定义已关闭。 You need to provide at least a valueField definition and create an instance of the store rather than passing it's name 您需要至少提供一个valueField定义并创建商店的实例,而不是传递其名称
     { {\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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM