如何在yii 1中使用radioButton

时间:2016-09-26 06:36:21

标签: php html5 yii1.x

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

  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 )。我需要将数据保存到数据库中,因为选择的选项标题不是数字。我该怎么办?

1 个答案:

答案 0 :(得分: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/