3

i have a foreach loop in my zend form, in this case the $this->args[1]has a count of 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) );
    }

the problem i encounter is that the $submitImage and $checkBox get overwritten and i only get one element of each, the last one.

any ideas how to make them all show up?

thanks

i've also tried:

$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++;
    }

but it doesn't work

Patrioticcow
  • 26,422
  • 75
  • 217
  • 337
  • 1
    You will have to give each element a unique name, or use subforms. See http://stackoverflow.com/questions/405897/zend-form-array-based-elements – drew010 Feb 25 '12 at 02:24

2 Answers2

4

your really close, only need minor fixes. Anything to make the name of the element unique.

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) );
    }

or if you like

$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 has the right of it I'm just more verbose :)

[EDIT] corrected variable collision in second loop.

RockyFord
  • 8,529
  • 1
  • 15
  • 21
  • the first foreach worked. the second one didn't for some reason, just in case someone else reads this answer. Lots of thanks – Patrioticcow Feb 27 '12 at 17:15
2

Your element names must be different.

 $submitImage = new Zend_Form_Element_Image($submitImage);
 $checkBox = new Zend_Form_Element_Checkbox($checkBox);
Liyali
  • 5,643
  • 2
  • 26
  • 40