简体   繁体   中英

how to add zend form elements to the form in php?

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

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.

Your element names must be different .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM