wolverine4277
15 Jan 2007, 2:31 PM
I have been trying work with YAHOO.ext.grid.Grid + PHP + XML and finally i got an example working.
All works fine but the performance maybe improved...
I don't know if some of the functions that i use can be improved somehow... for example the function colum_name() or maybe another way using less queries, i use 3 query, one for the total count of records, another for obtain the column name, and the last for the data that must be showed in the grid...
<test.php>
<?php
function total_count() {
if ($resultado_mysql = mysql_query('SELECT COUNT(*) FROM items')) {
if ($fila = mysql_fetch_row($resultado_mysql)) {
return $fila[0];
} else {
// Something usefull for the xml that contains the error
}
} else {
// Something usefull for the xml that contains the error
}
}
function column_name($column_index) {
$result = @mysql_query('SHOW COLUMNS FROM items');
if ($result) {
if ($row = @mysql_fetch_row($result)) {
if (@mysql_data_seek($result, $column_index)) {
return $row[$column_index];
} else {
// Something usefull for the xml that contains the error
}
} else {
// Something usefull for the xml that contains the error
}
} else {
// Something usefull for the xml that contains the error
}
}
function to_xml($sql_query) {
if ($resul = @mysql_query($sql_query)) {
$xml_doc = "<?xml version='1.0' encoding='iso-8859-1' ?>";
$xml_doc.= "\n<Result>\n";
$xml_doc.= "\n<Items>\n";
while($row = @mysql_fetch_assoc($result)) {
$xml_doc.= "\t<Item>\n";
while(list($key, $value) = each($row)) {
$xml_doc.= "\t\t<" . $key . ">" . $value . "</" . $key . ">\n";
}
$xml_doc.= "\t</Item>\n";
}
$xml_doc.= "</Items>";
$xml_doc.= "\n<TotalCount>" . total_count() . "</TotalCount>";
$xml_doc.= "\n</Resultado>";
} else {
// Something usefull for the xml that contains the error
$xml_doc = ...
}
return $xml_doc;
}
$xml_doc = '';
$offset = $_POST['pageSize'];
$from = $offset*($_POST['page'] - 1);
if ($conn = @mysql_connect('server', 'user', 'password')) {
if (@mysql_select_db('test')) {
$sql_query = 'SELECT *' . "\n";
$sql_query.= 'FROM items' . "\n";
if (isset($_POST['sortDir']) && !empty($_POST['sortDir'])) {
$sql_query.= 'ORDER BY ' . column_name($_POST['sortColumn']) . ' ' . $_POST['sortDir']. "\n";
}
$sql_query.= 'LIMIT ' . $from . ', ' . $offset;
$xml_doc = to_xml($sql_query);
} else {
// Something usefull for the xml that contains the error
$xml_doc = ...
}
} else {
// Something usefull for the xml that contains the error
$xml_doc = ...
}
header('Content-type: text/xml');
echo $xml_doc;
?>
All works fine but the performance maybe improved...
I don't know if some of the functions that i use can be improved somehow... for example the function colum_name() or maybe another way using less queries, i use 3 query, one for the total count of records, another for obtain the column name, and the last for the data that must be showed in the grid...
<test.php>
<?php
function total_count() {
if ($resultado_mysql = mysql_query('SELECT COUNT(*) FROM items')) {
if ($fila = mysql_fetch_row($resultado_mysql)) {
return $fila[0];
} else {
// Something usefull for the xml that contains the error
}
} else {
// Something usefull for the xml that contains the error
}
}
function column_name($column_index) {
$result = @mysql_query('SHOW COLUMNS FROM items');
if ($result) {
if ($row = @mysql_fetch_row($result)) {
if (@mysql_data_seek($result, $column_index)) {
return $row[$column_index];
} else {
// Something usefull for the xml that contains the error
}
} else {
// Something usefull for the xml that contains the error
}
} else {
// Something usefull for the xml that contains the error
}
}
function to_xml($sql_query) {
if ($resul = @mysql_query($sql_query)) {
$xml_doc = "<?xml version='1.0' encoding='iso-8859-1' ?>";
$xml_doc.= "\n<Result>\n";
$xml_doc.= "\n<Items>\n";
while($row = @mysql_fetch_assoc($result)) {
$xml_doc.= "\t<Item>\n";
while(list($key, $value) = each($row)) {
$xml_doc.= "\t\t<" . $key . ">" . $value . "</" . $key . ">\n";
}
$xml_doc.= "\t</Item>\n";
}
$xml_doc.= "</Items>";
$xml_doc.= "\n<TotalCount>" . total_count() . "</TotalCount>";
$xml_doc.= "\n</Resultado>";
} else {
// Something usefull for the xml that contains the error
$xml_doc = ...
}
return $xml_doc;
}
$xml_doc = '';
$offset = $_POST['pageSize'];
$from = $offset*($_POST['page'] - 1);
if ($conn = @mysql_connect('server', 'user', 'password')) {
if (@mysql_select_db('test')) {
$sql_query = 'SELECT *' . "\n";
$sql_query.= 'FROM items' . "\n";
if (isset($_POST['sortDir']) && !empty($_POST['sortDir'])) {
$sql_query.= 'ORDER BY ' . column_name($_POST['sortColumn']) . ' ' . $_POST['sortDir']. "\n";
}
$sql_query.= 'LIMIT ' . $from . ', ' . $offset;
$xml_doc = to_xml($sql_query);
} else {
// Something usefull for the xml that contains the error
$xml_doc = ...
}
} else {
// Something usefull for the xml that contains the error
$xml_doc = ...
}
header('Content-type: text/xml');
echo $xml_doc;
?>