doukun1450
2016-09-26 06:36
浏览 92
已采纳

如何在yii 1中使用radioButton

In order to display the list of options to the users, I used following code:

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

and

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

But, these codes are saving data as the form of number (eg. if user chooses Large company and org it saves it to the database as 0, or if user chooses miduim company and org it saves it to the database as 1). I need to save data to the database as choosen option title not as number. How can i do it?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doushao6874 2016-09-26 06:44
    已采纳

    You need to convert $data like below:-

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

    Note:- instead of data numbers will goes in your case because your array is look like :- Array(0=>'Large Company & Organisation',1=>'Medium Company & Organisation' ....... so on)

    Reference taken:- http://www.yiiframework.com/forum/index.php/topic/10621-radio-button/

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题