编程语言
首页 > 编程语言> > 如何在php中将zend表单元素添加到表单中?

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

作者:互联网

我的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++;
    }

但它不起作用

解决方法:

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

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拥有它的权利我只是更加冗长:)

[编辑]
在第二个循环中纠正变量碰撞.

标签:php,foreach,zend-framework,zend-form
来源: https://codeday.me/bug/20190826/1730675.html