extjs mysql例子 初级_extjs load data from mysql with php example

loadData.php:

require_once 'lib/response.php';

class fruit

{

var $fruit_id;

var $name;

var $variety;

}

$host = "192.168.1.105";

$user = "jet";

$pass = "^^^^^";

$mydb = "fruitdb";

$con=mysqli_connect($host,$user,$pass,$mydb);

// Check connection

if (mysqli_connect_errno())

{

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

$result = mysqli_query($con,"SELECT * FROM fruit");

$query_array=array();

$i=0;

//Iterate all Select

while($row = mysqli_fetch_array($result))

{

//Create New User instance

$frt = new fruit();

//Fetch User Info

$frt->fruit_id=$row['fruit_id'];

$frt->name=$row['name'];

$frt->variety=$row['variety'];

//Add User to ARRAY

$query_array[$i]=$frt;

$i++;

}

//Creating Json Array needed for Extjs Proxy

$res = new Response();

$res->success = true;

$res->message = "Loaded data";

$res->data = $query_array;

//Printing json ARRAY

print_r($res->to_json());

mysqli_close($con);

?>

response.php:

/**

* @class Response

* A simple JSON Response class.

*/

class Response {

public $success, $data, $total,$message, $errors, $tid, $trace;

public function __construct($params = array()) {

$this->success = isset($params["success"]) ? $params["success"] : false;

$this->message = isset($params["message"]) ? $params["message"] : '';

$this->data = isset($params["data"]) ? $params["data"] : array();

}

public function to_json() {

return json_encode(array(

'success' => $this->success,

'message' => $this->message,

'data' => $this->data

));

}

}

store:

Ext.define('AM.store.Grid', {

extend: 'Ext.data.Store',

model: 'AM.model.Grid',

autoLoad: true,

pageSize: 5, // numero de registros por Grid

proxy: {

type: 'ajax',

api: {

read: 'data/loadData.php',

},

reader: {

type: 'json',

root: 'data',

successProperty: 'success'

},

writer: {

type: 'json',

root: 'data',

encode: true

}

}

});

model:

Ext.define('AM.model.Grid', {

extend: 'Ext.data.Model',

idProperty: 'userID',

fields: [{name: 'fruit_id', type: 'int'},'name', 'variety']

});

view:

Ext.define('AM.view.Grid' ,{

extend: 'Ext.grid.Panel',

alias: 'widget.mygrid',

title: 'All Users',

initComponent: function() {

this.store = 'Grid';

this.columns = [

{header: 'Fruit_id', dataIndex: 'fruit_id', flex: 1},

{header: 'Name', dataIndex: 'name', flex: 1},

{header: 'Variety', dataIndex: 'variety', flex: 1}

];

this.callParent(arguments);

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值