如何使用单选按钮更新特定行上的数据数组?代码点火器



我正在制作一个后端测试应用程序,后端用户可以在其中更改答案或选择。

我希望单选按钮在更改时将值设置为1或者更新我的答案或选择,并将以前的选择替换为0

我有answers表,这是我的输入示例

correct字段是每次用户更改选项或答案时我都要更新的字段预期的结果是,如果我更新正确的选择,则前一个选择将被0取代。新的选择或答案将在正确字段中更新为1

id | answers | question_id | correct
1  |   A     |     1       |   0
2  |   B     |     1       |   1
3  |   C     |     1       |   0
4  |   D     |     1       |   0
5  |  F.1    |     2       |   0
6  |  F.2    |     2       |   1

更新模型-所以我已经可以更新或更改答案文本的数组,但我不熟悉ID的更新和替换值。

public function update_post(){
$id = $this->input->post('answers_textId');
$data = $this->input->post();
for($i = 0; $i < (count($data['id'])); $i++){
$batch[] = array(
'id' => $data['id'][$i],
'answer' => $data['answer'][$i],
'correct' => $data['correct_id'][$i]
);
}
return $this->db->update_batch('answers', $batch, 'id');
} 

在视图

这是id

<input type="hidden" name="answers_textId" value="<?php echo $answer['question_id']; ?>" />  //For the question_id field
<input type="hidden" name="id[]" value="<?php echo $answer['id']; ?>" /> //For the id or PK

这是我的单选按钮,它的元素是opt_1opt_2等等

<input type="radio" class="form-check-input" name="opt_<?php echo $answer['id']; ?>" value="1">

这是我的文本字段或文本框

<h5>Choice: </h5><input type="text" name="answer[]" class="form-control" value="<?php echo $answer['answer']; ?>" />

你就是昨天的那个人:(

我假设你已经向控制器发送了数据

您需要question_id和id(来自答案表(

首先更新all correct=false或0,其中question_id=$id(问题id(

这将使问题0的所有答案都正确,例如question_id=1有4个答案,它将使所有4个答案都正确=0

然后更新correct=1,其中id=answer_table_id(PK(并且question_id=$id(问题的id(

这将更新您更改的答案,更正为=1你不需要question_id,因为id(答案表pk(是唯一的

所以现在试着做这个函数。

最新更新