public function actionAddquestions() { $curModel = null; foreach (Yii::$app->request->post() as $name => $val) { echo htmlspecialchars($name . ': ' . $val) . "<br />"; $model = null; if (substr($name, 0, 8) == 'question') { $model = new ExamQuestion(); $model->Question = $val; $model->ExamId = $_POST['ExamId']; $sql = "SELECT MAX(ExamQuestionId) AS ExamQuestionId FROM exam_question"; $max_id = ExamQuestion::findBySql($sql)->one(); $model->ExamQuestionId = $max_id->ExamQuestionId + 1; $model->save(); $curModel = $model; } else { if (substr($name, 0, 10) == 'choicedesc') { $model = new ExamQuestionChoices(); $model->ChoiceDescription = $val; $model->IsRightChoice = $_POST['isrightchoice' . substr($name, 10, 12)]; $model->ExamQuestionId = $curModel->ExamQuestionId; $sql = "SELECT MAX(ExamQuestionChoicesId) AS ExamQuestionChoicesId FROM exam_question_choices"; $max_id = ExamQuestionChoices::findBySql($sql)->one(); $model->ExamQuestionChoicesId = $max_id->ExamQuestionChoicesId + 1; $model->save(); } } } return $this->redirect(['/site/feed']); }