Hi. I have these models:
-
Section (this holds the information of every section)
-
SubjSchedule (this holds all the schedules of every subject)
-
SubjSection (which holds all the subject schedule in each section)
I want to add a subject schedule in a certain section, so I have a dropdownlist that generates all sections available. Then from that, I will generate a checkboxlist that will show all schedules of every subject.
I created this in my subj-section/_form.php
<div class="subj-section-form">
<?php $form = ActiveForm::begin(); ?>
//generates all sections in a dropdownlist
<?= $form->field($model, 'section_id')->dropdownList(
ArrayHelper::map(Section::find()->all(), 'section_id', 'section_code')
) ?>
<?php $data = ArrayHelper::map($data, 'subjsched_id', 'subjsched_summary'); ?>
//this the checkboxlist
<?= $form->field($model, 'subjsection')->checkboxList($data, [
'class' => "btn-checkbox options",
'data-toggle' => "button",
'item' => function($index, $label, $name, $checked, $value) {
$checked = $checked ? 'checked' : '';
return "<p><input type='checkbox' {$checked} name='{$name}' value='{$value}'>{$label}</p>";
}])->label('Choose Subjects');?>
This is my subj-section/create.php
public function actionCreate()
{
$model = new SubjSection();
$data = SubjSchedule::find()->all();
if ($model->load(Yii::$app->request->post())){
$subjectsectionList = $_POST['SubjSection']['subjsection'];
foreach($subjectsectionList as $value){
$newmodel = new SubjSection();
$newmodel->section_id = $model->section_id;
$newmodel->subjsched_id = $value;
$newmodel->save();
}
$searchModel = new SubjSectionSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
return $this->render('create', [
'model' => $model,
'data' => $data,
]);
}
[b]My problem is when I want to update the subject schedules under a section, the checkboxes are not selected. I found out from another thread that I just have to put this:
$model->subjsection = ArrayHelper::map(SubjSection::find()->where(['section_id' => $id])->all(), 'subjsect_id', 'subjsched_id');
.
However, I need to check which section was chosen from the dropdownlist but I can’t get the value. How do I get the selected value from the dropdownlist so I could use it as a filter on my checkboxlist?
thank you so much in advance![/b]