我正在构建一个管理表单,以插入一个包含问题和选项的新测验。每个测验都有多个问题,每个问题只能有一个选项类型(' trueorfalse',' monthsoftheyear' - >这些都在显示端呈现,所以我不需要创建多个选项,因为它们是可预测的)或多个自定义选项(因此optiontype =" CUSTOM"对于该问题,每个选项都有自己的文本字段)。
这就是数据库的样子:
QUESTION
id / text / ... other irrelevant fields
13 / this is the true or false question / ...
14 / this is a custom question / ...
CHOICES
id / question_id / optiontype / text
24 / 13 / trueorfalse / null
25 / 14 / CUSTOM / red
26 / 14 / CUSTOM / blue
27 / 14 / CUSTOM / ...and so on fir the other custom options
function save($f3){
// save the user's results to the database
$newquiz = new Quizzes ($this->db);
$newquiz->copyfrom('POST', function($val) {
return array_intersect_key($val, array_flip(array('name','description')));
});
$newquiz->save();
$question = new Questions ($this->db);
$choice = new Choices ($this->db);
foreach($f3->get('POST.question') as $q) {
$question->reset();
$text = $q['text'];
$byline = $q['byline'];
$correctanswer = $q['correctanswer'];
$answerwriteup = $q['answerwriteup'];
$optiontype = $q['optiontype'];
$question->quiz_id_fk = $newquiz->_id;
$question->text = $text;
$question->byline = $byline;
$question->correctanswer = $correctanswer;
$question->answerwriteup = $answerwriteup;
$question->save();
$questionid = $question->id;
if ($optiontype != 'CUSTOM') {
$choice->reset();
$choice->question_id_fk = $questionid;
$choice->optiontype = $optiontype;
$choice->save();
}
// everything above this comment works perfectly fine. It's the section below that I have trouble with:
if ($optiontype == 'CUSTOM') {
$choice->reset();
$choice->question_id_fk = $questionid;
$choice->optiontype = $optiontype;
// how do I get the text fields for each option
$choice->save();
}
}
$f3->reroute('/admin');
}
}
admin页面上的insert表单有一个SELECTtype输入字段。管理员可以选择" trueorfalse"或者"月"或者" CUSTOM"作为选择。如果他们选择" trueorfalse"或者"月",没有进一步的输入。但是,如果他们选择CUSTOM,我使用jquery为CUSTOM选项取消隐藏另外6个文本字段(为简单起见,假设总有6个选项)。然后,管理员可以键入CUSTOM选项的文本(' red',' green'等)。请参阅下图以供参考。
我的问题:如何处理CUSTOM选项保存到数据库?我认为它应该是某种foreach循环,但我不知道究竟要循环什么。
这是表单代码(下面有一些jquery,我已经排除了它并没有真正影响代码逻辑):
<loop from="{{ @i=1 }}" to="{{ @i < 11 }}" step="{{ @i++ }}">
<img src="{{@BASE}}/app/views/images/black.jpg" width="100%" height="1px">
<label class="text-right col-sm-3" for="question{{@i}}">Question {{@i}}</label>
<div class="controls col-sm-9">
<input name="question[{{@i}}][text]" id="question{{@i}}_text" type="hidden">
<div id="editor_{{@i}}" style="height:100px; margin-bottom: 20px "></div>
</div>
<label class="text-right col-sm-3" >Q{{@i}} Byline</label>
<div class="controls col-sm-9">
<input id="question1_byline " type="text" class="form-control " name="question[{{@i}}][byline]">
</div>
<label class="text-right col-sm-3" >Q{{@i}} Correct Answer</label>
<div class="controls col-sm-9">
<input type="text" class="form-control " name="question[{{@i}}][correctanswer]">
</div>
<label class="text-right col-sm-3" >Q{{@i}} Answer Writeup</label>
<div class="controls col-sm-9">
<input name="question[{{@i}}][answerwriteup]" id="question{{@i}}_answerwriteup" type="hidden">
<div id="editor_answer{{@i}}" style="height:100px; margin-bottom: 20px "></div>
</div>
<label class="text-right col-sm-3" for="question{{@i}}_optiontype">Q{{@i}} Option Type</label>
<div class="controls col-sm-9">
<select id="question{{@i}}_optiontype" class="form-control" data-role="select" name="question[{{@i}}][optiontype]">
<option value="100percent">100percent</option>
<option value="trueorfalse" selected>trueorfalse</option>
<option value="realorfake" selected>realorfake</option>
<option value="months">months</option>
<option value="CUSTOM">CUSTOM</option>
</select>
</div>
<label class="text-right col-sm-3" >Q{{@i}} CUSTOM OPTIONS (only if CUSTOM was selected)</label>
<div class="controls col-sm-9" id="customoption{{@i}}" style="display:none">
<div class="col-xs-2"><input type="text" class="form-control " name="question[{{@i}}][custom]"></div>
<div class="col-xs-2"><input type="text" class="form-control " name="question[{{@i}}][custom]"></div>
<div class="col-xs-2"><input type="text" class="form-control " name="question[{{@i}}][custom]"></div>
<div class="col-xs-2"><input type="text" class="form-control " name="question[{{@i}}][custom]"></div>
<div class="col-xs-2"><input type="text" class="form-control " name="question[{{@i}}][custom]"></div>
<div class="col-xs-2"><input type="text" class="form-control " name="question[{{@i}}][custom]"></div>
</div>
</loop>