开发者

I want to create drop down box in input.phtml. (don't use zend_form)

开发者 https://www.devze.com 2023-02-09 07:47 出处:网络
I want to create drop down box in input.phtml. I want to Create drop down box. select d开发者_高级运维ata from database.

I want to create drop down box in input.phtml.

I want to Create drop down box. select d开发者_高级运维ata from database. and use get select data by function fetchAll() (joinLeft,where,order)

Case 1 : Don't use zend_form (Because i use table layout page input.phtml)

Case 2 : use zend_form

I want to answer two case and example , The answer as to better ?

Thank for Answer


Well, do this

in you view phtml file you can direcly call db table model

UPDATE

On your controller

$model = new Model_Somemodel();
$modelvalues = $model->fetchAll();

if(count($modelvalues) > 0)
    $this->view->modelvalues = $modelvalues

And on your view file

<select>
<?php  if($this->modelvalues): ?>
    <?php foreach($this->modelvalues as $value)
        echo "<option>".$value->somefiled."</option>";
     ?>     
<?php endif; ?>
</select>


Case 1 :

In your view :

echo $this->formSelect('name', 'Option 1', array(), 
    array('Option 1', 'Option 2'));

1st arg : name of the select, 2nd arg : value; 3rd arg : attributs, 4th arg : options

Case 2 :

class MyForm extends Zend_Form
{
    function init()
    {
        $this->addElement('select', 'my_select', 
            array('label' => 'My select', 
           'multioptions' => array('Option 1', 'Option 2'), value => 'Option 1'));
    }
}

In your controller :

$this->view->form = new MyForm();

In your view :

// Render the form opening tag
echo $this->form->renderForm(false);
echo '<table>';
echo '<tr>';
echo '<th>'
// Render only the label
echo $this->form->my_select->renderLabel();
echo '</th>
echo '<td>';
// Render only the select
echo $this->form->my_select->renderViewHelper();
echo '</td>';
echo '</tr>';
echo '</table>';
echo '</form>';

As you can see, Zend_Form is very flexible. So USE IT.

You can use Zend_Db::fetchPairs to fetch your options from the database.


First case - You can use formSelect helper in your view.
Second case - use Zend_Form_Element_Select in your Zend_Form.


I want to recover the value of the val (2nd arg)

formSelect('name', '**Option 1**', array(), 
    array('Option 1', 'Option 2'));

1st arg: name of the select, 2nd arg: value; 3rd arg: attributs, 4th arg: options

in input.phtml.

0

精彩评论

暂无评论...
验证码 换一张
取 消