<?php
/**
* This example show how "advanced" form can be proccessed.
* Task:
* We need form to add some devices to shopping catalog.
* Device has sereral interfaces, and prices for each interface.
* Also we have several groups of customers. Each group has his special price.
* Uuf ! :)
* It's not so hard as sound !
*
*/
$start = utime();
include './Base.lib.php';
include './FormControls.lib.php';
include './FormControlSets.lib.php';
/* customers groups id and name */
$groups_id = array('1'=>'Group 1', '2'=>'Group 2', '3'=>'Group 3');
/* device interfaces */
$interfaces = array('rs'=>'RS232', 'wand'=>'Wand emulation', 'usb'=>'USB');
/* device prices */
$prices = array(
'rs'=>array(1=>10,20, 30),
'wand'=>array(1=>15,25,35),
'usb'=>array(1=>20,30,40)
);
/* create form */
$Form = new TableForm($PHP_SELF);
$Form->setTableAttr(array('border'=>1, 'cellpadding'=>4, 'cellspacing'=>0));
$Form->setTableSize(4);
$Form->setCellAttr('bgcolor', '#f1edc7');
$Form->add(new CData('Add device to catalog'), '', '', 4,'', array('align'=>'center'));
$DeviceCode = new TextField('code', 'C1300', 'Device code');
$DeviceCode->setReg('/^\w{4,30}$/i');
$Form->addFieldWithLabel($DeviceCode, '','','','','',3);
$Form->addFieldWithLabel(new TextField('name', 'Barcode reader', 'Device name'), '','','','','',3);
$Form->add(new CData('Prices'), '','', 4, '', array('align'=>'center'));
$Form->add(new CData('Cutomers groups'), 1, '', 3, '', array('align'=>'center'));
$Form->add(new CData('Iterfaces'));
foreach ($groups_id as $id=>$group) {
$Form->add(new CData($group));
}
foreach ($prices as $intrf=>$groups) {
$Form->add(new CData($interfaces[$intrf]));
foreach ($groups as $group=>$price) {
$Price = new TextField("price[$intrf][$group]", $price, "Price for $groups_id[$group]", array('size'=>4), 'double');
$Price->setRange(1, 1000);
$Form->add($Price);
unset($Price);
}
}
$Buttons = new Bin('span');
$Buttons->add(new Reset(''));
$Buttons->add(new Submit('', 'Save'));
$Form->add($Buttons, '','',4,'',array('align'=>'center'));
/* print (and valid) form */
if (!$Form->isSubmit()) {
/* show form first time */
$Form->view();
} else {
/* form was subitted */
if (!$Form->valid()) {
/* form filled incorect - show error mesage */
echo $Form->getErrMsg();
/* show form again */
$Form->view();
} else {
/* get form values */
$res = $Form->getValue();
/* OK here our data */
echo "<pre>";
print_r($res);
echo "</pre>";
}
}
echo "<br>Page create in: ".(utime()-$start)." sec.<br>";
function utime ()
{
$time = explode( " ", microtime());
$usec = (double)$time[0];
$sec = (double)$time[1];
return $sec + $usec;
}
?>
|