version 2.2.1

  PClib - PHP component library


Ex.4: Extending PClib

In this example we will create new class based on PCLib grid class. We want add possibility of checkboxes in database grid. For this purpose, we create new method print_checkbox() which will print checkbox html code and overload grid methods grid->print_element() which is called for printing each template tag and method grid->out() which do whole grid output. Here is the code: 7.1 gridform class
class gridform extends grid {
    switch (
$this->elements[$id]['type']) {
'check'$this->print_checkbox($id$sub$value); break;
parent::print_element($id$sub$value); break;

out() //overload
$action $this->elements['grid']['action'];
"<form method=\"post\" action=\"$action\">";

print_checkbox($id$sub$value) {
$rowid $this->getvalue('ID');
$ch = ($value == '1')? 'checked' '';
"<input type=\"checkbox\" name=\"${id}[$rowid]\" value=\"1\" $ch>";
In method print_element() we call our own handler for element of type check. For other elements we use default handling.
In method out() we just add form tags around generated html.
print_checkbox() print html code of the checkbox. It takes 3 arguments: $id is element name (identificator) specified in elements section, $sub is tag additional parameter (for example "lb" or "err") - not important here - and $value is value of element coming from values array. Method is called for each row of grid, at place where new check tag is used. We assume that ID of table row is in field ID. Posted data can be found in array $_POST['id_of_element'];
Example of template:
7.1 Example of formgrid template. File basket.tpl
class grid name "basket" pglen "30" action "order.php"
string ID
check BUY
string NAME lb "Product"
string PRICE lb "Price" format "0.,"
pager pager size "10"
<table border="1">
<!-- BLOCK items -->
<!-- BLOCK END -->

<div class="pager">Page {pager.active} of {pager.ptotal} {pager.pages}</div>
<input type="submit" name="submit" value="Buy items">
Note that you must put {ID} (i.e. table rowid) into items block, because we use it in our method print_checkbox().
Using this approach you can add new elements also at classes based on form class or tpl class.

«PREV Using form