2015-04-14 27 views
0

檢索多個複選框值我試圖顯示從數據庫中的值,但未能顯示,如何在Magento

首先,我使用複選框插入值,

代碼如下所示,

$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)

任何一個請幫助解決這個! !!!!!

回答

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