version 2.8.8

  PClib - PHP component library


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
class grid name "productlist"
string NAME lb "Product" sort
string PRICE lb "Price" format "0.,"
pager pager size "10" pglen "30"
<table border="1">
{BLOCK items}
<tr><td>{NAME}</td><td>{PRICE} USD.</td></tr>
<tr><td colspan="2">There are no items.</td></tr>
<tr><td colspan=2>{pager}</td></tr>
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 PCDb('pdo_mysql://root@localhost/test');  //Open database

$productlist = new PCGrid ('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.
= new grid ('tpl/productlist.tpl''productlist');

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

/* Newlines at SQL below are important! */

"select NAME,PRICE from PRODUCTS where 1


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;

«PREV Basic template | Using form NEXT»