Though I’ve already discuss how to create dropdown in one of my previous article, however here I would discuss it a bit in detail.
You can create dropdown using the following code.
<?php
class CustomForm extends Zend_Form
{
public function init()
{
$this->setMethod(‘post’);
$this->setAction(‘user/process’);
$country = $this->createElement(‘select’,'countries’);
$country ->setLabel(‘Countries:’)
->addMultiOptions(array(
‘US’ => ‘United States’,
‘UK’ => ‘United Kingdom’
));
}
}
Or can also be created using the following code.
<?php
class CustomForm extends Zend_Form
{
public function init()
{
$this->setMethod(‘post’);
$this->setAction(‘user/process’);
$country = new Zend_Form_Element_Select(‘countries’);
$country ->setLabel(‘Countries:’)
->addMultiOptions(array(
‘US’ => ‘United States’,
‘UK’ => ‘United Kingdom’
));
}
}
The options are hard code in both of the cases. This will not be requirement always.
Sometime or most of the time application need that drop down should be populated fetching records from the data base. So for this purpose I would like to implement the following code.
Fist in your model write the following.
<?php
class Countries extends Zend_Table
{
protected $_name = “countries”;
public function getCountriesList()
{
$select = $this->_db->select()
->from($this->_name,
array(‘key’ => ‘id’,'value’ => ‘country_name’));
$result = $this->getAdapter()->fetchAll($select);
return $result;
}
}
Keep in mind that the records we fetch will have an associated array having id as key and country name as value. If you don’t defined the above array, array(‘key’ => ‘id’,'value’ => ‘country_name’), as I have defined, you will not get what you want. So be careful while fetching records for constructing drop down.
And now in your form write the following code.
<?php
class CustomForm extends Zend_Form
{
public function init()
{
$this->setMethod(‘post’);
$this->setAction(‘user/process’);
$countries = new Countries();
$countries_list = $countires->getCountriesList();
$country = new Zend_Form_Element_Select(‘countries’);
$country ->setLabel(‘Countries:’)
->addMultiOptions( $countriesList
));
}
}
In the above code the only thing changes is
- I have instantiated model using
$countries = new Countries();
2. Call getCountiresList() method. And assigned that list to the drop down, instead of assigning hard coded values.
Your example works perfectly but what happens when you need to pass more data to your select statement inside your CustomForm? I need to say in Class Countries ->where(‘country_id != ?’, $country_id).
Thanks by the way your tutorials rock.
Thanks again.
nice example
I have a table of houses, with a row incentiveId, which relates to a table of Incentives. I am trying to present a list of those houses, including a dropdown list of the values of the incentiveId taken from the incentives table. I’ve set up the table files to include the relational mappings and such, for some reason, I can’t get the values for the incentives to pull into my index view of the tables. Here is the HousingController class which has the index action:
class HousingController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$this->view->headTitle(“Housing”);
$houses = new Application_Model_DbTable_Housing();
$this->view->houses = $houses->fetchAll();
$incentives = new Application_Model_DbTable_Incentive();
$this->view->incentives = $incentives->getIncentivesList();
}
which calls the get incentives list, from my incentives table:
class Application_Model_DbTable_Incentive extends Zend_Db_Table_Abstract
{
protected $_name = ‘incentive’;
protected $_dependentTables = array(‘Application_Model_DbTable_Housing’);
public function getIncentivesList()
{
$select = $this->_db->select()
->from($this->_name,
array(‘key’ => ‘id’, ‘value’=>’incentive’));
$result = $this->getAdapter()->fetchAll($select);
return $result;
}
and my housing table class with the relational mappings:
class Application_Model_DbTable_Housing extends Zend_Db_Table_Abstract
{
protected $_name = ‘housing’;
protected $_referenceMap = array(
‘StatesId’ => array(
‘columns’ => ‘statesId’,
‘refTableClass’ => ‘states’,
‘refColumns’ => array(‘id’)
),
‘IncentivesId’ => array(
‘columns’ => ‘incentivesId’,
‘refTableClass’ => ‘incentive’,
‘refColumns’ => array(‘id’)
)
);
public function readHouse($id) {
$id = (int)$id;
$row = $this->fetchRow(‘id = ‘ . $id);
if (!$row) {
throw new Exception(“ERROR: Could not find entry id $id in Housing table”);
}
return $row->toArray();
}
I am able to return the incentivesId from the housing table, but I am unable to recursively return the incentives id and incentive for each row of the housing table in a drop down box. I was wondering if you had any pointers on the functionality behind that.
I have an drop down list for age. Now that list is rather long (1-90). Would it be possible to construct that list (the array) using a for loop?
This is a nice example, I agree…
But you have 2 syntax error:
you should change “countires” by “coutries”
Thanks again for this source code
I also made a mistake.
you should change “countires” by “countries”