如何在 yii 1 中使用单选按钮

How to use radioButton in yii 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).我需要将数据作为选择的选项标题而不是数字保存到数据库中。我该怎么做?

您需要像下面这样转换 $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/