Example #1
0
$grid->addColumn("name", "NAME", "string");
$grid->addColumn("firstname", "FIRSTNAME", "string");
// add an "integer" and a "double" column
// you can specifiy the unit: double(m), the precision: double(2), or both: double(m,2)
// these will be used in the default renderer NumberCellRenderer
$grid->addColumn("age", "AGE", "integer");
$grid->addColumn("height", "HEIGHT", "double(m,2)", null, true, null, false);
// add column with predefined values, organized in "option groups" (dropdown list)
$grid->addColumn("country", "COUNTRY", "string", array("Europe" => array("be" => "Belgium", "fr" => "France", "uk" => "Great-Britain", "nl" => "Nederland"), "America" => array("br" => "Brazil", "ca" => "Canada", "us" => "USA"), "Africa" => array("ng" => "Nigeria", "za" => "South-Africa", "zw" => "Zimbabwe")));
// add some other columns: email, url, boolean, date
$grid->addColumn("email", "EMAIL", "email");
// $grid->addColumn("website", "WEBSITE", "url");
$grid->addColumn("freelance", "FREELANCE", "boolean");
$grid->addColumn("lastvisit", "LAST VISIT", "date");
// action column ("html" type), not editable
$grid->addColumn("action", "", "html", NULL, false);
// load data from csv
$handle = fopen("demo.csv", "r");
$data = array();
while ($row = fgetcsv($handle, 0, ";")) {
    if (count($row) <= 1 || $row[0] == 'id') {
        continue;
    }
    $data[] = array("id" => $row[0], "name" => $row[1], "firstname" => $row[2], "age" => $row[3], "height" => $row[4], "continent" => $row[5], "country" => $row[6], "email" => str_replace("*****@*****.**", "Nam@quisdiamluctus." . (isset($_GET['json']) ? "json" : "xml") . ".php.org", $row[7]), "freelance" => $row[8] == '1', "lastvisit" => $row[9], "website" => $row[10]);
}
// render XML or JSON
if (isset($_GET['json'])) {
    $grid->renderJSON($data);
} else {
    $grid->renderXML($data);
}
    return $rows;
}
// Database connection
$mysqli = mysqli_init();
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
$mysqli->real_connect($config['db_host'], $config['db_user'], $config['db_password'], $config['db_name']);
// create a new EditableGrid object
$grid = new EditableGrid();
/* 
*  Add columns. The first argument of addColumn is the name of the field in the databse. 
*  The second argument is the label that will be displayed in the header
*/
$grid->addColumn('id', 'ID', 'integer', NULL, false);
$grid->addColumn('name', 'Name', 'string');
$grid->addColumn('firstname', 'Firstname', 'string');
$grid->addColumn('age', 'Age', 'integer');
$grid->addColumn('height', 'Height', 'float');
/* The column id_country and id_continent will show a list of all available countries and continents. So, we select all rows from the tables */
$grid->addColumn('id_continent', 'Continent', 'string', fetch_pairs($mysqli, 'SELECT id, name FROM continent'), true);
$grid->addColumn('id_country', 'Country', 'string', fetch_pairs($mysqli, 'SELECT id, name FROM country'), true);
$grid->addColumn('email', 'Email', 'email');
$grid->addColumn('freelance', 'Freelance', 'boolean');
$grid->addColumn('lastvisit', 'Lastvisit', 'date');
$grid->addColumn('website', 'Website', 'string');
$grid->addColumn('action', 'Action', 'html', NULL, false, 'id');
$mydb_tablename = isset($_GET['db_tablename']) ? stripslashes($_GET['db_tablename']) : 'demo';
$result = $mysqli->query('SELECT *, date_format(lastvisit, "%d/%m/%Y") as lastvisit FROM ' . $mydb_tablename);
$mysqli->close();
// send data to the browser
$grid->renderJSON($result);