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);
}
});