QF + QFC + Smarty + DB
| From: | Julien Huang | Date: | Sun, 15 Aug 2004 15:32:25 +0000 |
| Subject: | QF + QFC + Smarty + DB | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-14122@lists.php.net to get a copy of this message | ||
Hello,
I have to generate a form according to data mined from a MySQL database.
The generated form have to be templated through Smarty. The whole thing
is nested into QFC.
Source code:
/****************************/
/*
** $tpl =& new Smarty;
** $db =& new DB;
*/
class SelectOfferPage extends HTML_QuickForm_Page
{
var $_db;
function SelectOfferPage($formName, $method = 'post', $target = '_self', $attributes = NULL, &$db)
{
$this->HTML_QuickForm_Page($formName, $method, $target, $attributes)
$this->_db =& $db;
}
function buildForm()
{
$res =& $this->_db->query('SELECT * FROM offers');
for ($offer_group = array(); $row =& $res->fetchRow(); )
$offer_group[] =& $this->createElement('checkbox', $row['offer_id'], NULL, $row['offer_name']);
$this->addGroup($offer_group, 'offer_id');
$this->addElement('submit', NULL, 'Submit');
}
}
class ActionDisplay extends HTML_QuickForm_Action_Display
{
var $_db, $_tpl;
function ActionDisplay(&$db, &$tpl)
{
$this->_db =& $db;
$this->_tpl =& $tpl;
}
function _renderForm(&$page)
{
$form_array =& $page->toArray();
$res =& $this->_db->query('SELECT * FROM offers');
for ($offer_info = array(); $row =& $res->fetchRow(); )
$offer_info[] = $row;
$this->_tpl->assign('form_array', $form_array);
$this->_tpl->assign('offer_info', $offer_info);
$this->_tpl->display('template.tpl');
}
}
$controller =& new HTML_QuickForm_Controller('offer_form');
$controller->addPage(new SelectOfferPage('select_offer_page', NULL, NULL, NULL, $db));
$controller->addAction(new ActionDisplay($db, $tpl));
$controller->run();
/****************************/
My problem comes from the SQL Query. It's a waste to do the same query two times.
I need the query to know how many checkboxes I need (in SelectOfferPage class), and
I need the query to ouput information about the offers (description, owner's
fullname, etc..) (in ActionDisplay class). The whole has to outputed into a table.
The form should look like this:
--------------------------------------
| Offer | Owner | Description | Date |
----------------------------------------
|x| Foo | A | ...... | d/m/y|----------------------------------------
|x| Bar | A | ...... | d/m/y|----------------------------------------
|x| Baz | B | ...... | d/m/y|----------------------------------------
| Submit |The "|x|" represents the checkboxes. I thought about building the table directly into SelectOfferPage, but it would come dirty. Any suggestion ? Thanks, -- Julien Huang