我的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拥有它的权利我只是更加冗长:)
[编辑]
在第二个循环中纠正变量碰撞.