I have a table called "number"
Has 2 fields "id" "number"
Has list of values
id number
1 one
2 two
3 three
4 four
5 five
6 six
How can i load all the items in table "number" into a form of checkboxlist
And how do i name the checkbox items
I have a table called "number"
Has 2 fields "id" "number"
Has list of values
id number
1 one
2 two
3 three
4 four
5 five
6 six
How can i load all the items in table "number" into a form of checkboxlist
And how do i name the checkbox items
You can use listData() like this:
CHtml::listData(Number::model()->findAll(), id, number)
number will become the label.
Insert the above as param 3 to checkBoxList() or activeCheckBoxList()
/Tommy
I put this code in my _forum.php and it did not displaying any thing
<?php CHtml::listData(Number::model()->findAll(), 'id', 'number') ?>
Code for _forum.php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'number-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'number'); ?>
<?php echo $form->textField($model,'number',array('size'=>20,'maxlength'=>20)); ?>
<?php echo $form->error($model,'number'); ?>
</div>
<div class="linear">
<?php echo CHtml::listData(Number::model()->findAll(), 'id', 'number'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
This is displaying me only teh word "Array" on the screen after the text box… did i any thing wrong?
I’m not sure about what you weant to accomplish but since you use CActiveForm this might be what you are looking for
<?php
echo $form->checkBoxList(
$model,
'someAttribute',
CHtml::listData(Number::model()->findAll(), 'id', 'number'),
);
?>
(not tested)
/Tommy
Thank You Very Much it worked!!!