2

为了向用户显示选项列表,我使用了以下代码:

  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>

但是,这些代码是以数字的形式保存数据(例如,如果用户选择 Large company and org,则将其保存到数据库中为 0,或者如果用户选择 miduim company and org,则将其保存到数据库中为 1)。我需要将数据作为选择的选项标题而不是数字保存到数据库中。我该怎么做?

4

1 回答 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/

于 2016-09-26T06:44:54.373 回答