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

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

the first foreach worked. the second one didn't for some reason, just in case someone else reads this answer. Lots of thanks
2

Your element names must be different.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.