phpdev phpdev - 2 months ago 13
PHP Question

How to use radioButton in yii 1

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?

Answer

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/

Comments