如何在Magento中检索多个Checkbox值

时间:2015-04-14 12:24:32

标签: php database magento checkbox

我尝试显示数据库中的值但无法显示。,

首先,我使用复选框。,

插入值

代码在下面,

$fieldset->addField('city', 'checkboxes', array(
                'label' => $this->__('City'),
                'name' => 'city[]',
                'required' => true,
                "checked" => $city,
                'values' => array(
                    array('value' => '0', 'label' => 'aaaaa'),
                    array('value' => '1', 'label' => 'bbbbbbb'),
                    array('value' => '2', 'label' => 'ccccccc'),
                    array('value' => '3', 'label' => 'dddddddd'),
                    array('value' => '4', 'label' => 'eeeeeeee')
                ),
                'onclick' => "",
                'onchange' => "",
                'disabled' => false,
                'value'  => '1',
                'tabindex' => 1
            )); 

之后,

$city = $post_data['city'] = implode(',', $post_data['city']);

使用上面的代码成功插入,如(0,1,2),

 if ($object->getData('city')) {
                $city = $object->getData('city');
                $city = explode(",", $city);
                 //var_dump($city);die;
            }

插入值我成功了, 现在我想显示选中的一个(0,1,2)

任何人请帮忙解决这个问题!!!!!!

1 个答案:

答案 0 :(得分:0)

创建一个新函数来获取选中的值

代码

$fieldset->addField('city', 'checkboxes', array(
                    'label' => $this->__('City'),
                    'name' => 'city[]',
                    'required' => true,
                    "checked" => $city,
                    'values' => $this->getOptionValues($city),
                    'onclick' => "",
                    'onchange' => "",
                    'disabled' => false,
                    'value'  => '1',
                    'tabindex' => 1
                ));

public function getOptionValues($city)
    {
        $result = array();
        $selectedvalues = $city; //exploded array values(array(0=>0,1=>1,2=>2))
        $optionslists = array(
            array('value' => '0', 'label' => 'aaaaa'),
            array('value' => '1', 'label' => 'bbbbbbb'),
            array('value' => '2', 'label' => 'ccccccc'),
            array('value' => '3', 'label' => 'dddddddd'),
            array('value' => '4', 'label' => 'eeeeeeee')
        );
        if (count($selectedvalues) > 1) {
            foreach ($selectedvalues as $selectedvalue) {
                foreach ($optionslists as $optionslist) {
                    if (in_array($selectedvalue, $optionslist)) {
                        $result[] = $optionslist;
                    }

                }
            }
        } else {
            $result = $optionslists;
        }
    return $result;
    }