To work in a manner that makes economize the net traffic and to have the possibility that its data set would be dinamic, could change at every time, ErgoDataGrid must rely on an Apache server and a MySql database.
Data also contained in database tables are trasferred to the ErgoDataGrid client only for the effective need in every filter situation, if a filter has been stabilited, for the sorting and to visualize only a predefined number of records at every instant, and must follow a server validation, that happens has reported on end.
For the client side the step to make activate server side functionalities is, in the Grid object creation:
- setting of dataUrl equal to the PHP page enstabilished for the data trasferring (see rows below).
On the server, PHP page with the data to trasfer through the browser must have a sintax equal to that of the example reported on end, with the following rules:
- must include dbutils.php and config.php files, the last one with 4 variables to set according to the utilized MySql server setting up:
|
mysql_host for the host name (host_name:door) on which MySql server run; mysql_db for the database name from where the data arrive; mysql_user for the user name whom access to the database; mysql_password for the accessing user password; |
|
$query is the string with the SELECT clause to pass to MySql for the data loading. it's possible to declare an $imgPath variable with the path into which search the image files for the columns with an ArrayGrid [col][2] equal to I. $ArrayFilter is an array with the table field names, present in the header row passed to the browser with the first print command, which determine what field to use for every ErgoDataGrid column filter (first column on the left determine the filter on the base of this array's field with index equal to 0, and so on); $ArrayFilterType contains an array with the definitions (a char every one) of the every ArrayFilter element field type, with the following possible values: S = String N = Number D = Date I = Image $ArraySortAsc is the array with the field names which determine ErgoDataGrid sorting when the final user click on the component visualized by Internet Explorer enstabilish that the filter must be ascendent, that is when after the click appear an up arrow near the column name; $ArraySortDesc is the array that enstabilish what fields determine decrescent sort; can be set to be equal to $ArraySortAsc; |
|
$isWhere=strstr(strtoupper($query)," WHERE ")!=FALSE; ConvertFilterFields(); //optional, needs for the grid's internalization. $out = PrintRecordCount("tableName",$isWhere);
|
| $result = mysql_query($query . Filter($isWhere,$filter) . OrderBy() . DbPage() ); |
| it's optional to use the internalization rules, valueing the $_POST["lang"] variable, which contains the abbrevation of the language choosen by the final user to visualize the grid and the dataset. |
<?php
include "../../config.php";
include "../dbutils.php";
$imgPath="cars/";
$ArrayFilter=array("img_url","id","description","displacement","registration","km","power","fuel","price_euro");
$ArrayFilterType=array("I","M","S","N","D","N","N","S","N");
$ArraySortAsc=array("img_url","id","description","displacement","registration","km","power","fuel","price_euro");
$ArraySortDesc=$ArraySortAsc;
$query="SELECT *,UNIX_TIMESTAMP(registration) AS Date2 FROM Cars";
$conversion_value=1.26;
if (isset($_POST["lang"]) && $_POST["lang"]=="it") $lang="it"; else $lang="en";
function ConvertFilterFields() {
global $lang,$conversion_value;
if (!isset($_POST["F"])) return;
$af=split("~",$_POST["F"]);
$i=0;
foreach($af as $ff) {
if ($i<>0) $f.="~";
if ($lang=="en") {
$fi=split("[|]",$ff);
for ($ii=0;$ii<count($fi);$ii++) {
if ($ii<>0) $f.="|";
if ($ii>1) {
if ($fi[0]==5) {
$f.=Round($fi[$ii]*1.609);
} else if ($fi[0]==7) {
if ($fi[$ii]=="Gasoline")
$f.="Benzina";
else
$f.="Diesel";
} else if ($fi[0]==8) {
$f.=Round($fi[$ii]/$conversion_value);
} else
$f.=$fi[$ii];
} else
$f.=$fi[$ii];
}
} else
$f.=$ff;
$i++;
}
$_POST["F"]=$f;
}
$link = mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_db);
$isWhere=strstr(strtoupper($query)," WHERE ")!=FALSE;
ConvertFilterFields();
$out=PrintRecordCount("Cars",$isWhere);
$result = mysql_query($query . Filter($isWhere) . OrderBy() . DbPage());
$out .= '"table":"Cars","value":[';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($lang=="it") {
$k=$row["km"];
$p=$row["price_euro"];
$f=$row["fuel"];
} else {
$k=round($row["km"]/1.609);
$p=round($row["price_euro"]*$conversion_value);
if ($row["fuel"]=="Benzina") $f="Gasoline"; else $f="Diesel";
}
$line= "['". $imgPath. $row["img_url"] . "','"
. $row["id"] . "','"
. $row["description"] . "','"
. $row["displacement"] . "','"
. ConvertDate(Date("l j F Y",$row["Date2"]),$lang) . "','"
. Date("d/m/Y",$row["Date2"]) . "','"
. Group($k) . "','"
. $k . "','"
. $row["power"] . "','"
. $f . "','"
. Group($p);
if ($lang=="it") $line.= " €"; else $line.=" $";
$line.= "','" . $p . "'],";
$out .= $line;
}
$out = preg_replace("/,$/is","", $out);
print "{" . $out . "]}";
if (isset($result)) {
mysql_free_result($result);
}
mysql_close($link);
?>
|