PDA

View Full Version : I cant display data in the grid with JSON and PHP



horrikhalid
17 Jul 2008, 10:12 AM
Hi guys I have a bit trouble to display data using the gridPanel with JSON and PHP,
the grid is displayed but without any data(:|
The js file named grid.js is like this:

Ext.onReady(function(){

Ext.QuickTips.init();
var xg = Ext.grid;
///// Reader ///////



var ds = new Ext.data.Store({
id : 'ds',
proxy : new Ext.data.HttpProxy({
url: 'getData.php',
method: 'POST'
}),
baseParams :{task: "LISTING"},
reader : new Ext.data.JsonReader({
root: 'results',
totalProperty: 'total',
id: 'id'
},[
{name: 'Marque', type: 'string', mapping: 'marque'},
{name: 'Tj', type: 'int', mapping: '3j'},
{name: 'Sj', type: 'int', mapping: '7j'},
{name: 'Ej', type: 'int', mapping: '10_15j'}
])
});

var colMod = new xg.ColumnModel([
new xg.RowNumberer(),
{header: "Voiture", width: 20, sortable: true, dataIndex: 'Marque'},
{header: "3j", width: 20, sortable: true, dataIndex: 'Tj'},
{header: "7j", width: 20, sortable: true, dataIndex: 'Sj'},
{header: "10-15j", width: 20, sortable: true, dataIndex: 'Ej'}
]);
////////////////////

///////////////////
var grid3 = new xg.GridPanel({
store: ds,
cm: colMod,
viewConfig: {
forceFit:true
},
width:600,
height:300,
title:'Grid with Numbered Rows and Force Fit',
iconCls:'icon-grid',
renderTo: document.body
});

});
The php file is (getData.php):

<?php
mysql_connect("localhost", "root", "") or
die("Could not connect: " . mysql_error());
mysql_select_db("db_name");

$task = '';
if ( isset($_POST['task'])){
$task = $_POST['task'];
}
switch($task){
case "LISTING":
getTarif();
break;
default:
echo "{failure:true}";
break;
}
function getTarif()
{
$query = "SELECT v.marque,t.id_voiture,t.3j,t.7j,t.10_15j FROM tarif t, voiture v WHERE v.id_voiture = t.id_voiture";
$result = mysql_query($query) or die(mysql_error());
$nbrows = mysql_num_rows($result);
if($nbrows>0){
while($rec = mysql_fetch_array($result)){

$arr[] = $rec;

}
$jsonresult = JEncode($arr);
echo '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
} else {
echo '({"total":"0", "results":""})';
}
}
function JEncode($arr){
if (version_compare(PHP_VERSION,"5.2","<"))
{
require_once("./JSON.php"); //if php<5.2 need JSON class
$json = new Services_JSON();//instantiate new json object
$data=$json->encode($arr); //encode the data in json format
} else
{
$data = json_encode($arr); //encode the data in json format
}
return $data;
}
?>

NOTE that the result of this file is legal JSON.

The index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="resources/css/ext-all.css"/>
<script type="text/javascript" src="adapter/ext/ext-base.js"></script>
<script src="ext-all.js" type="text/javascript"></script>
<script type="text/javascript" src="grid.js"></script>
</head>

<body>
<div id="test"></div>
</body>
</html>


any help is appreciated

regards Khaled

horrikhalid
17 Jul 2008, 10:49 AM
I'm sorry
I forget to load the store, So Now there is no problem