我想在input.phtml中创建下拉框。 (不要使用zend_form)

时间:2011-02-06 06:21:40

标签: zend-framework

我想在input.phtml中创建下拉框。

我想创建下拉框。从数据库中选择数据。 并使用函数fetchAll()获取选择数据(joinLeft,where,order)

案例1:不要使用zend_form(因为我使用表格布局页面input.phtml)

案例2:使用zend_form

我想回答两个案例和例子 ,答案更好?

感谢答案

4 个答案:

答案 0 :(得分:1)

好吧,这样做

在你查看phtml文件时你可以直接调用db table model

<强>更新

在您的控制器上

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

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

在您的观看文件

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

答案 1 :(得分:0)

第一种情况 - 您可以在视图中使用formSelect助手 第二种情况 - 在Zend_Form_Element_Select中使用Zend_Form

答案 2 :(得分:0)

案例1:

在您看来:

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

1st arg:select的名称,2nd arg:value; 3rd arg:attributs,4th arg:options

案例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'));
    }
}

在您的控制器中:

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

在您看来:

// 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>';

如您所见,Zend_Form非常灵活。所以使用IT

您可以使用Zend_Db :: fetchPairs从数据库中获取选项。

答案 3 :(得分:0)

我想恢复val(2nd arg)的值

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

1st arg:select的名称,2nd arg:value; 3rd arg:attributs,4th arg:options

在input.phtml中