How to dynamically populate list box from database?
How to dynamically populate list box fr开发者_如何学Com database? Please Provide some examples in zend framework.
Say if you have a table called Test
and a corresponding model for it named Test
,
class Test extends Zend_Db_Table {
protected $_name = "Test";
function getLisItems() {
$select = $this->getAdapter()->select()->from($this->_name, array(
'key' => 'column1_key',
'value' => 'column2_value'
));
return $this->getAdapter()->fetchPairs($select);
}
}
In your controller action or view, have the following code.
$t = new Test();
$list = new Zend_Form_Element_Select('list');
$list->setLabel('Select your item: ')
->addMultiOptions($t->getLisItems());
Now your list box will be populated with items from the database.
精彩评论