2016-09-26 54 views
2

爲了顯示的選項給用戶的列表中,我用下面的代碼:如何警予使用單選1

public function getData($property) 
    { 
     $data=array(
      'membership_fee' => array(
       'Large Company & Organisation', 
       'Medium Company & Organisation', 
       'Small Company & Organisation ', 
       'Mini- Company & Organisation', 
       'Individual Membership ', 
       'Large Company & Organisation ', 
       'Medium Company & Organisation ', 
       'Small Company & Organisation', 
       'Mini- Company & Organisation ', 
       'Individual Membership ', 
      ), 

     ); 
     return $data[$property]; 
    } 

<div class="form-group"> 
    <?= $form->labelEx($model, 'membership_fee', array('class' => 'col-xs-12 col-sm-4 control-label')) ?> 
    <div class="col-xs-12 col-sm-8"> 
     <?= $form->radioButtonList($model, 'membership_fee', $model->getData('membership_fee'), array(
      'template' => '<div class="radio col-xs-12 col-sm-6">{beginLabel}{input}{label}{endLabel}</div>', 
      'separator' => '', 
     )); ?> 
     <?= $form->error($model, 'membership_fee') ?> 
    </div> 
</div> 

但是,這些代碼保存數據數字的形式(例如,如果用戶選擇大型公司並將其保存到數據庫中爲0,或者如果用戶選擇miduim公司並將其保存到數據庫爲1)。我需要將數據保存到數據庫,因爲選擇標題不是數字。我該怎麼做?

回答

2

你需要轉換$data象下面這樣: -

$data=array(
    'membership_fee' => array(
    'Large Company & Organisation'=>'Large Company & Organisation', 
    'Medium Company & Organisation'=>'Medium Company & Organisation', 
    'Small Company & Organisation'=>'Small Company & Organisation', 
    'Mini- Company & Organisation'=>'Mini- Company & Organisation', 
    'Individual Membership'=>'Individual Membership', 
    'Large Company & Organisation'=>'Large Company & Organisation', 
    'Medium Company & Organisation'=>'Medium Company & Organisation', 
    'Small Company & Organisation'=>'Small Company & Organisation', 
    'Mini- Company & Organisation'=>'Mini- Company & Organisation', 
    'Individual Membership'=>'Individual Membership', 
), 

注: - 而不是數據的數字會在你的情況去,因爲你的陣列是這樣的: - Array(0=>'Large Company & Organisation',1=>'Medium Company & Organisation' ....... so on)

參考採取: - http://www.yiiframework.com/forum/index.php/topic/10621-radio-button/

+0

很高興幫助你phpdev :) :) –