如何在php中将zend表单元素添加到表单中?

时间:2012-02-25 01:53:14

标签: php zend-framework foreach zend-form

我的zend形式有一个foreach循环,在这种情况下$this->args[1]的计数为5:

foreach ($this->args[1] as $val)
    {

        $submitImage = new Zend_Form_Element_Image('submit_image');
        $checkBox = new Zend_Form_Element_Checkbox('id_checkbox');

        $this->addElement( $submitImage ->setImage($val->full_path) );
        $this->addElement( $checkBox ->setValue($val->id) );
    }

我遇到的问题是$submitImage$checkBox被覆盖,我只得到每个元素,最后一个元素。

如何让它们全部出现?

感谢

我也尝试过:

$i=0;
foreach ($this->args[1] as $val)
    {

        $submitImage = 'submitImage'.$i;
            $checkBox = 'checkBox'.$i;

        $submitImage = new Zend_Form_Element_Image('submit_image');
        $checkBox = new Zend_Form_Element_Checkbox('id_checkbox');

        $this->addElement( $submitImage ->setImage($val->full_path) );
        $this->addElement( $checkBox ->setValue($val->id) );
    $i++;
    }

但它不起作用

2 个答案:

答案 0 :(得分:4)

你真的很亲密,只需要很小的修复。任何使元素名称唯一的东西。

foreach ($this->args[1] as $val)
    {

        $submitImage = new Zend_Form_Element_Image('submit_image'. $val->id);
        $checkBox = new Zend_Form_Element_Checkbox('id_checkbox' . $val->id);

        $this->addElement( $submitImage ->setImage($val->full_path) );
        $this->addElement( $checkBox ->setValue($val->id) );
    }

或者如果你喜欢

$i=0;
foreach ($this->args[1] as $val)
    {

        $image = 'submitImage'.$i;
            $box = 'checkBox'.$i;

        $submitImage = new Zend_Form_Element_Image($image);
        $checkBox = new Zend_Form_Element_Checkbox($box);

        $this->addElement( $submitImage ->setImage($val->full_path) );
        $this->addElement( $checkBox ->setValue($val->id) );
    $i++;
    }

Liyali拥有它的权利我只是更加冗长:)

<强> [编辑] 在第二个循环中修正了变量碰撞。

答案 1 :(得分:2)

您的元素名称必须不同

 $submitImage = new Zend_Form_Element_Image($submitImage);
 $checkBox = new Zend_Form_Element_Checkbox($checkBox);