dongle7882 2014-11-30 09:00
浏览 21
已采纳

提交给定次数的相同表单:为什么此代码不起作用?

So I am making a quiz in which the user gets confronted with a succession of questions which he answers through a form. The series of problems each contain a given number of questions, and the questions get asked one after the other when the user validates.

I am therefore trying to re-render the view with the form for each problem until they're all done. This is my action:

public function actionAnswer($id_serie)
{
    if ($id_serie != 0)                          //getting the serie's info
    {
        $serie = Serie::find()
            ->where(['id' => $id_serie])
            ->one();
        $problems = (new \yii\db\Query())        //getting the problems in the serie
            ->select('*')
            ->from('problems')
            ->where(['id_serie' => $id_serie])
            ->all();
        $prob_counter = $serie->nbr_of_problems; //counts the number of questions answered
        $id_serie = 0;
    }
    $model = new Answer;
    if ($model->load(Yii::$app->request->post()) && $model->validate())
    {
        $model->save();     // works just fine every time
        if (--$prob_counter <= 0)
        {
            return $this->redirect('index.php?r=student/entry');
        }
    }
    return $this->render('answer',
            ['model' => $model,
            'problems' => $problems,
            'serie' => $serie,
            'prob_counter' => $prob_counter,       //these last two are for debug
            'id_serie' => $id_serie]);
}

When this action gets executed the first time, $id_serie is never null or =0. Hence I am using this to query the db only once and set a counter to the total number of problems in the serie. (id est the number of time the user has to submit the form) If his answer is valid, I decrement my counter and if it falls under 0, there are no questions to answer anymore and the user gets redirected.

However, this counter never goes down to 0: it is set correctly, it is decremented only once, and then it never falls lower, no matter where I put the line. (inside or outside any loop) On the other hand the data from the form is properly inserted in the db each time.

What am I getting wrong?

  • 写回答

1条回答 默认 最新

  • doucan8276 2014-11-30 19:53
    关注

    As per your code, $prob_counter just stores the number of problems for each series. You need to change this to show the number of unanswered problems for the series. How you implement this will depend on your models and database but it should be something like:

    $problems = (new \yii\db\Query())        //getting the problems in the serie
            ->select('*')
            ->from('problems')
            ->where(['id_serie' => $id_serie])
            ->andWhere('not exists (select id from answer where problemid = problems.id')
            ->all();
    

    Also you should probably look at working with relational data and avoid using Query() in the above section.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效