Assigning multi options to drop down in Zend framework

  • SumoMe

Today I will show you how to add multi options to select tag. It is simple, just add this code to your controller.

    public function testAction() {
        //we are creating Zend_Form instance
        $dropdown = new Zend_Form_Element_Select("dropdown");
        //set label for select tag
        $dropdown->setLabel("Option:");
        //set options
        $dropdown->setMultiOptions(array(
            "Select 1", "Select 2", "Select 3", "Select 4"
        ));
        //add form to the view
        $this->view->form = $form->addElement($dropdown);
    }

And in the view of the controller, show the form.

echo $this->form;

If you check the code, you will see this:

<select name="dropdown" id="dropdown">
    <option value="0" label="Select 1">Select 1</option>
    <option value="1" label="Select 2">Select 2</option>
    <option value="2" label="Select 3">Select 3</option>
    <option value="3" label="Select 4">Select 4</option>
</select>

If you want to specify the values of option tags then use this array:

array(
“1” => “Select 1”,
“2” => “Select 2”,
“3” => “Select 3”,
“4” => “Select 4”
)

And the result will be:

<select name="dropdown" id="dropdown">
    <option value="1" label="Select 1">Select 1</option>
    <option value="2" label="Select 2">Select 2</option>
    <option value="3" label="Select 3">Select 3</option>
    <option value="4" label="Select 4">Select 4</option>
</select>

And that’s all. Good luck.

Leave a Reply


*