English
version 2.2.1

  PClib - PHP component library

•TOC

Ex.2: Using grid

For our grid example we use a little modified template 3.1. We use table layout and add some template elements. 4.1 Example of grid template. File productlist.tpl
<?elements
class grid name "productlist"
string NAME lb "Product" sort
string PRICE lb "Price" format "0.,"
pager pager size "10" pglen "30"
?>
<h1>{PAGE_TITLE}</h1>
<table border="1">
<tr><th>{NAME.lb}</th><th>{PRICE.lb}</th>
<!-- BLOCK items -->
<tr><td>{NAME}</td><td>{PRICE} USD.</td></tr>
<!-- BLOCK END -->
<BR>
<!-- BLOCK noitems -->
<tr><td colspan="2">There are no items.</td></tr>
<!-- BLOCK END -->
<tr><td colspan=2>{pager}</td></tr>
</table>
Go <a href="javascript:history.back();">back</a>
If we examine this template, we found just a few news. There is new element pager. It will be transformed into clickable selector of pages, if we have long list of data. Attribute size tells how many page-links should be shown and another attribute pglen set length of page to 30 rows.
Another new element is class. It is used for global configuration of whole grid. Here, only internal name of the grid is set.
Behind this elements we can see two string elements. We already know them from basic template. There is a new attribute lb which define label of the field. It will replace template tag {NAME.lb} and {PRICE.lb}, respectively. You can use labels for almost any element types. These dot-separated tag modificators are used in another cases too. Try {pager.next}, {pager.prev}, {pager.total} etc. See also reference guide for complete list of tags and modificators.
Moreover in field NAME is attribute sort. It marks column NAME as sortable. {NAME.lb} will be replaced with clickable link, which sorts the grid. If you want sorting by the PRICE too, add sort atribute to this field.
Now, the code. 4.2 Printing grid.
require 'pclib/pclib.php';                           //Include pclib framework
session_start();                                     //Use sessions
$pclib->db = new db('mysql://root@localhost/test');  //Open database

$productlist = new grid ('tpl/productlist.tpl''productlist'); //We load HTML  of productlist page
$productlist->setquery('select NAME,PRICE from PRODUCTS');      //Set sql-query
$productlist->values['PAGE_TITLE'] = 'Product list';             //Set some additional tags

$productlist->out();                                            //And show product-list on the screen.
 


Pretty easy, isn't it?
At third line we connect to database and register database object as $pclib->db, so grid can access it.
Second parameter of grid constructor ('productlist') is optional. If present, grid state (such as current sort column, selected page and filter) will be stored into session, so you can go through pages and don't loose grid settings.

PClib rarely stores anything into session, and most of features will work without session enabled. In our example, grid properties will be saved in $_SESSION['grid']['productlist'] variable. For more details about session storing, look at method grid::savesession().

Method $page->setquery() set datasource for the grid data. Here we get columns NAME and PRICE from PRODUCTS table, so you can use tags {NAME} and {PRICE} in template.
You can apply filter on the grid by setting grid->filter property - see below. 4.2 Filtering grid.
$productlist 
= new grid ('tpl/productlist.tpl''productlist');

$productlist->filter['MAXPRICE'] = 1000;
$productlist->filter['CATEGORY'] = 'Meat';

/* Newlines at SQL below are important! */

$productlist->setquery(
  
"select NAME,PRICE from PRODUCTS where 1
  ~ AND PRICE <= [MAXPRICE]
  ~ AND CATEGORY='[CATEGORY]'"
);

$productlist->out();
 


Placeholders [MAXPRICE] and [CATEGORY] will be replaced with values from $productlist->filter array. If value for placeholder does not exists, whole line in SQL query will be omitted. Array $productlist->filter is stored in session, which means that filter will be remembered until you change it or delete whole filter by setting $productlist->filter = null; See also dynamic sql in reference guide.

«PREV Basic template | Using form NEXT»