How To Create A Drop Down List

Hi,

I have two tables student,and student group. In student table there has student group_id, which is foreign key of Student group table. Now i want a list where group name will be drop down and ASC order.

In the _form page of student folder write code


<div class="row">

<?php echo $form->labelEx($model,'Group_ID'); ?>

<?php echo $form->dropDownList($model,'StudentGroup', CHtml::listData(StudentGroup::model()->findAll(), 'Group_Id', 'Group_Name'), array('empty'=>'Select location'));?>

<?php echo $form->error($model,'Group_ID'); ?>

</div>

It shows the following error

Property "student.StudentGroup" is not defined.

how to solve this.

Please help me as soon as possible.

what is the name of your model file is it StudentGroup.php? Hope so you are not making mistake in upper/lower case. Since you want to arrange them in ASC order define $criteria something like this


<?php echo $form->dropDownList($model,'StudentGroup', CHtml::listData(StudentGroup::model()->findAll(array('order' => 'Group_Name ASC'), 'Group_Id', 'Group_Name'), array('empty'=>'Select location'));?>

instead of $model, ‘StudentGroup’ it should be $model, ‘Group_Id’ or ‘Group_Name’

Hi,

i think it’s because Yii tries to find the field ‘StudentGroup’ in your model

try to replace it by student Group_Id :


<?php echo $form->dropDownList($model,'Group_Id', CHtml::listData(StudentGroup::model()->findAll(), 'Group_Id', 'Group_Name'), array('empty'=>'Select location'));?>

and eventually change its label in your student model :


public function attributeLabels()

	{

		return array(

			'...' => '...',

			'Group_Id' => 'Student Group',

		);

	}

Thanks to every one.