Form/Checkbox design question

Hi all,

I am new to Yii and this forum; so thank you first for your sparing your time to help me.

I am unsure how I can achieve the following in yii:

I have a many-to-many table, namely: EXAMS and LEVELS. What I want to do is create a form in which I can set the EXAMS that belong to each LEVEL. In the Create page of Level_has_Exams (the many-to-many table), I pass in the LevelID as a variable. Then under it there is a list of Checkboxes of all avaliable EXAMS. The code I’ve got so far is:

The FORM:


		foreach ($exams as $e=>$exam) {

			

			$temp = $exam->idExam;	

		

			echo CHtml::activeCheckBox($exam, "[$temp]idExam"); 

			echo $exam->ExamName;

			echo $form->error($exam,"[$temp]idExam");

			echo "<br />";		

		}

The CONTROLLER:


	$exams = $ExamDataProvider->data;

	foreach ($exams as $e=>$exam) {

			$temp = $exam->idExam;	

			if($_POST['Exam'][$temp]['idExam']==1)

			{	

				$model->Exam_idExam = $temp;

				$model->save();					

				$model=new LevelHasExam;

				$model->Level_idLevel = $idLevel;

			}

		}



The few problems/questions I am facing are:

  1. How can I set the checkbox to be ‘checked’ or ‘unchecked’ at first?

  2. Is it correct to deal with the checkboxes this way? Or should I use a checkbox list?

Thank you for your time!