Re: Quick_Form - setting default value for a radio group
| From: | Bertrand Mansion | Date: | Tue, 16 Sep 2003 21:15:36 +0000 |
| Subject: | Re: Quick_Form - setting default value for a radio group | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-7851@lists.php.net to get a copy of this message | ||
<grahamc@53tech.com> wrote :
> Hi - I have a series of radio button groups crated as follows and I'm not
> able to have the default values be checked.
>
> foreach ( $r['response_set'] as $r_i ) {
>
> $scaled_items[] = & $form->createElement('radio',
> 'item_'.$r['item_id']. '[response_no]', NULL, NULL,
> $r_i['response_no']);
>
> }
>
>
> $form->addGroup($scaled_items, 'scaled_items'.$key, $r['item_id'] .
> ' - '
> . $r['item_text'], NULL, false);
>
>
> This creates a series of scaled survey questions in a form. For example:
>
> "Did the student demonstrate creative problem solving?" [ ] Not at
> all [ ] A little bit [ ] Kind of [ ] Most of the time [ ] Always.
> Each [ ] is a radio button with the only difference between the 5 being the
> value ( values are integers 1-5 ).
>
>
> So far I've tried adding the attributes array like this
> $scaled_items[] = & $form->createElement('radio',
> 'item_'.$r['item_id']. '[response_no]', NULL, NULL,
> $r_i['response_no'],
> array('checked'=>'checked'));
>
> I've also tried using $form->setDefaults($my_defaults); and changing the
> $appendName parameter to true.
Hi,
I'd do it like that:
$labels = array('Not at all',
'A little bit',
'Kind of',
'Most of the time',
'Always');
$questions = array('Did you feed the cat ?',
'Did you vote for W. Bush ?');
foreach ($questions as $index => $question) {
$radio = array();
foreach ($labels as $value => $label) {
$radio[] =& HTML_QuickForm::createElement('radio', null, null,
$label, $value);
}
$form->addGroup($radio, 'question_'.$index, $question, '<br />');
$defaults['question_'.$index] = 0;
}
$form->setDefaults($defaults);
Bertrand Mansion
Mamasam