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:

country
   CountryID
   RegionID
   Name
   Description
   Population
   PictureURL
   democraty
regions
   RegionID
   Name
democracy
   did
   description

// 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

ob_start();
// 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 */
$dgrid->setUniqueNames("_abc");
/*** wich layout we want: 0-tabular(default), 1-columnar */
$dgrid->setLayouts(array("view"=>0));
/*** set for all columns 'nowrap' parameter */
$dgrid->setColumnsNoWrap("nowrap");
/*** set css class, default: "default" */
$dgrid->setCssClass("default");

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


// View Mode Settings:
// ---------
/*** set view mode settings: */
$dgrid->setColumnsInViewMode(array(
"Name" =>array("header"=>"Country","type"=>"label"),
"Region" =>array("header"=>"Region","type"=>"label"),
"PictureURL" =>array("header"=>"Image","type"=>"link",
"href"=>"http://www.yahoo.com", "target"=>"_new"),
"isdemocraty" =>array("header"=>"Is Democraty","type"=>"label")
));

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

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




« Back to description

No comments: