PDA

View Full Version : [SOLVED] Xml Mysql Grid problem



vaucer
29 Dec 2009, 11:26 AM
Hello all,

I try to display data in a simple grid with xmlreader. I use a php file for generate my xml doc.

For my php file all is ok, my DataStore request "baseParams:{task: "LISTING"}" work fine and i see in the console panel of firebug that my script "dataesibes.php" is correctly executed and getback a correct xml doc.

But my grid don't work, i think that the problem is probably situated around the xmlreader. I had many search on the forum but i don't find the solution.



I use extjs 3.1

My php script "dataesibes.php":


<?php

////////////////////////////////////////////////////////
// DATABASE.PHP - TUTORIAL PART 2
////////////////////////////////////////////////////////

// This will connect us to our database...
mysql_connect("localhost", "root", "") or
die("Could not connect: " . mysql_error());
mysql_select_db("sibes");

// The ext grid script will send a task field which will specify what it wants to do
$task = '';
if ( isset($_POST['task'])){
$task = $_POST['task'];
}
switch($task){
case "LISTING":
getList();
break;
default:
echo "{failure:true}";
break;
}

function getList()
{
$query = "SELECT * FROM edition WHERE id";
$result = mysql_query($query);
$nbrows = mysql_num_rows($result);
if($nbrows>0){

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/xml");
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$xml .= "<cont>";
$xml .= "<total>$nbrows</total>";
while ($rec = mysql_fetch_array($result)) {
$xml .= "<idr><![CDATA[".utf8_encode($rec['id'])."]]></idr>";
$xml .= "<titres><![CDATA[".utf8_encode($rec['idtitreincident'])."]]></titres>";
$xml .= "<descr><![CDATA[".utf8_encode($rec['titreincident'])."]]></descr>";
}
$xml .= "</cont>";
echo $xml;


} else {
echo '({"total":"0", "results":""})';
}
}


?> The XML doc returned:


<cont>
<total>13</total>
<idr>14</idr>
<titres>PSPI</titres>
<descr>Connexion au serveur impossible</descr>
<idr>2</idr>
<titres>CREST</titres>
<descr>Changement mot de passe</descr>
<idr>3</idr>
<titres>CICS</titres>
<descr>Blocage applicatif</descr>
<idr>5</idr>
<titres>SAA</titres>
<descr>Perte du serveur</descr>
<idr>7</idr>
<titres>STELINK</titres>
<descr>Problème de decodage</descr>
<idr>8</idr>
<titres>STEP2</titres>
<descr>Fichier FVR ou DRR absent</descr>
<idr>9</idr>
<titres>CICS</titres>
<descr>Perte de la partition SYSA</descr>
<idr>10</idr>
<titres>TELEX</titres>
<descr>Lignes telex inactive</descr>
<idr>1</idr>
<titres>-------------</titres>
<descr></descr>
<idr>16</idr>
<titres>CREST</titres>
<descr>Connexion serveur impossible</descr>
</cont>My main script JS "esibes.js":


var SibesDataStore;
var SibesColumnModel;
var SibeListingEditorGrid;
var SibeListingWindow;

Ext.onReady(function(){

Ext.QuickTips.init();

SibesDataStore = new Ext.data.Store({
id: 'SibesDataStore',
proxy: new Ext.data.HttpProxy({
url: '../script_PHP/sibes/dataesibes.php',
method: 'POST'
}),
baseParams:{task: "LISTING"}, // this parameter is passed for any HTTP request


reader: new Ext.data.XmlReader({
record: 'cont',
totalProperty: 'total',
id: 'idr'
},[
{name: 'ID', type: 'int', mapping: 'idr'},
{name: 'Compo', type: 'string', mapping: 'titres'},
{name: 'Titre', type: 'string', mapping: 'descr'}
]),
sortInfo:{field: 'ID', direction: "ASC"}
});

SibesColumnModel = new Ext.grid.ColumnModel(
[{
header: '#',
readOnly: true,
dataIndex: 'ID',
width: 50,
hidden: false
},{
header: 'Composant',
dataIndex: 'Compo',
width: 260,
editor: new Ext.form.TextField({
allowBlank: false,
maxLength: 390,
maskRe: /([a-zA-Z0-9\s]+)$/
})
},{
header: 'Titre incident',
dataIndex: 'Titre',
width: 180,
editor: new Ext.form.TextField({
allowBlank: false,
maxLength: 390,
maskRe: /([a-zA-Z0-9\s]+)$/
})
}]
);
SibesColumnModel.defaultSortable= true;

SibeListingEditorGrid = new Ext.grid.EditorGridPanel({
id: 'SibeListingEditorGrid',
store: SibesDataStore,
cm: SibesColumnModel,
renderTo: 'grid_esibes',
width: 1102,
height: 350,
enableColLock:false,
clicksToEdit:1,

selModel: new Ext.grid.RowSelectionModel({singleSelect:false})
});

SibesDataStore.load();

});
Many thank if you can give me a little help or just a link to a solution already post in the forum.

It's very important for me.

And very sorry for my very bad english i hope that you understand me :)

Mike Robinson
29 Dec 2009, 3:28 PM
There is really only one systematic way to approach this ...


Desk-check the host-side code. Make sure that for a correctly-formed request you get the correct XML output.
Now, desk-check the ExtJS configuration to be certain that it conforms to that XML in every, cAsE sEnSiTiVe respect.
Finally, be sure that the proper ExtJS components are correctly "wired together" to do the job.

ExtJS cares diddly-squat about what kind of host it is talking to, and even less about what kind of database the host may be talking to.

One thing that is immediately suspicious to me about your XML is that it contains "repeating items" with nothing that wraps each of them into appropriate groups. It's just a run-on stream of items.

I would have expected something like: (using quasi-English and all of this just for illustration...)

<response>
<record-count>2</record-count>
<records>
<record>
<idr>14</idr>
<titres>PSPI</titres>
<descr>bah</descr>
</record>
<record>
<idr>15</idr>
<titres>ABCD</titres>
<descr>foo</descr>
</record>
</records>
</response>You notice that in this version, everything is neatly nested. I suspect that the output from your host-side code is expected to be done the same way. "Well-formed XML documents" always look like this.

vaucer
30 Dec 2009, 3:52 AM
Thank you very much Mike :D

Effectively the problem was situtated on the structure of the doc xml generate.

After correction in your way, all is ok.

In fact at the first i used JSON encode but in french my encoding UTF-8 and ISO8859-1 not working.

I use PHP 4.3 so JSON is not natively supported, i used PEAR library JSON.php single file with "require once" but it did not work with UTF-8 encoding in this way. But with xml and utf8_encode() php function now all is ok

for information for future user encountering the same problem i post my script php corrected:



<?php

////////////////////////////////////////////////////////
// DATABASE.PHP - TUTORIAL PART 2
////////////////////////////////////////////////////////

// This will connect us to our database...
mysql_connect("localhost", "root", "") or
die("Could not connect: " . mysql_error());
mysql_select_db("sibes");

// The ext grid script will send a task field which will specify what it wants to do
$task = '';
if ( isset($_POST['task'])){
$task = $_POST['task'];
}
switch($task){
case "LISTING":
getList();
break;
default:
echo "{failure:true}";
break;
}

function getList()
{
$query = "SELECT * FROM edition WHERE id";
$result = mysql_query($query);
$nbrows = mysql_num_rows($result);
if($nbrows>0){

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/xml");
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$xml .= "<responses>";
$xml .= "<total>$nbrows</total>";
while ($rec = mysql_fetch_array($result)) {
$xml .= "<response>";
$xml .= "<idr>".utf8_encode($rec['id'])."</idr>";
$xml .= "<cont>";
$xml .= "<titres>".utf8_encode($rec['idtitreincident'])."</titres>";
$xml .= "<descr>".utf8_encode($rec['titreincident'])."</descr>";
$xml .= "</cont>";
$xml .= "</response>";
}
$xml .= "</responses>";
echo $xml;


} else {
echo '({"total":"0", "results":""})';
}
}
?>
Thank you very much for your precious help Mike Robin =D>=D>=D>=D>=D>=D>

Vaucer