Monday, August 28, 2006

Examples of code PHP DataGrid v.1.x.x

In datagrid.class file you can find commented lines of how to create, call and work with this class.

All you need is to copy all commented lines of the code into you page and uncomment some lines. That's all!

Now we'll check this example to get you understanding how it works.
For example, we have a database (mySQL) with 3 tables:


// creating variables that we need for database connection
$DB_USER='root'; // often like this: prefix_name
$DB_PASS=''; // must be already enscrypted
$DB_HOST='localhost'; // often localhost
$DB_NAME='thebutto_test'; // often like this: prefix_name

// now we create connection with our database
$db_conn=mysql_connect($DB_HOST,$DB_USER,$DB_PASS); mysql_select_db($DB_NAME,$db_conn);

// create sql statment (any SELECT statment you want)
// it's recomended to put Primary Key at the first place (CountryID in this example)
$sql="SELECT ".
" countries.CountryID, ".
" countries.Name, ".
" regions.Name as Region, ".
" countries.Description, ".
" countries.Population, ".
" countries.PictureURL, ".
" democraty.description as isdemocraty ".
" FROM countries ".
" INNER JOIN regions ON countries.regionID=regions.regionID ".
" LEFT OUTER JOIN democraty ON countries.democraty=democraty.did ";

// creating a new instance of DataGrid class
$dgrid = new DataGrid();

// linking data source with DataGrid
$dgrid->dataSourse($db_conn, $sql);

// General Settings
// ---------
/*** here we give unique names for HTML elements */
/*** wich layout we want: 0-tabular(default), 1-columnar */
/*** set for all columns 'nowrap' parameter */
/*** set css class, default: "default" */

// Sorting & Paging Settings:
// ---------
/*** allow sorting on columns: true/false; default - true */
/*** allow paging option: true/false; default - true */
/*** set paging settings: on top & bottom
array("results"=>true, "results_align"=>"left",
"pages"=>true, "pages_align"=>"center",
"page_size"=>true, "page_size_align"=>"right"),

// View Mode Settings:
// ---------
/*** set view mode settings: */
"Name" =>array("header"=>"Country","type"=>"label"),
"Region" =>array("header"=>"Region","type"=>"label"),
"PictureURL" =>array("header"=>"Image","type"=>"link",
"href"=>"", "target"=>"_new"),
"isdemocraty" =>array("header"=>"Is Democraty","type"=>"label")

/*** set debug mode = true/false(default) $ messaging = true(default)/false */
$dgrid->bind(false, false);

Now, let's see what we've got:

« Back to description

No comments: