有没有办法在表的一个字段中插入多个数据,并且一个特定的数据在其字段上具有不同的值?代码点火器

我正在制作一个测验应用程序,其中一个问题必须是正确的。我已经可以在我的表中插入多个数据,我遇到的问题是,我有一个特定的选择或答案,必须与其他选择或答案不同。

该应用程序的屏幕截图以及我想要做什么

http://img3.mukewang.com/635b759b000186a216791038.jpg

这是我的桌子的样子


table answers


id | answer | correct

 1 |  Apple |     1

 2 |  Mango |     0

 3 |  Melon |     0

1 表示正确答案,0 表示不正确。


所以这是我的模型。


我试图尝试获取作为值的单选按钮值并将1其插入数据库,但结果是,如果我添加另一个数据或多个数据。索引 0 或第一个数据是唯一可以插入的数据。我不能选择我可以检查的单选按钮,只能检查我可以检查的第一个按钮。

// Insert questions


    $field_question = array(

        'question'=>$this->input->post('question'),

    );


    $this->db->insert('questions', $field_question);


    // Insert Answers

    $data_answer = $this->input->post('choice[]');

    $data_is_correct = $this->input->post('checkChoice[]');



    $value = array();


    for($i = 0; $i < count($data_answer); $i++) {

        $value[$i] = array(

            'answer' => $data_answer[$i],

            'correct' => $data_is_correct[$i],

            'question_id' => $this->db->insert_id(),

        );

    }



    $this->db->insert_batch('answers', $value);


    if($this->db->affected_rows() > 0){

        return true;

    }else{

        return false;

    }

如果我添加 3 个新数据,我的表会出现问题


id | answer | correct

 1 |  Apple |     1

 2 |  Mango |     0

 3 |  Melon |     0

* New 3 Data Inserted 

 4 | Orange |     1

 5 | Tomato |     0

 6 | Grapes |     0

我不能做出Tomato或Grapes成为我的答案,也不能将其作为值 1 仅橙色或第一个添加的数据。


烙印99
浏览 145回答 1
1回答

繁星点点滴滴

动态字段脚本使变量val作为 checkChoice 的值,如下所示...&nbsp; <script>&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;$(document).ready(function(){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var i=1;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#add').click(function(){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var val =i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i++;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$('#dynamic_field').append(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'<tr id="row'+i+'">'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'<td><input type="hidden" name="choiceHid[]" value="0" /></td>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'<td><input type="text" name="choice[]" placeholder="Enter your Choice" class="form-control" /></td>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'<td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove"><span class="iconify" data-icon="dashicons:remove" data-inline="false"></span></button></td>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'<td>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<div class="form-check">'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<input class="form-check-input" type="radio" name="checkChoice" id="checkChoice" value="'+val+'" />'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<label class="form-check-label" for="exampleRadios1">'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Make this as an Answer'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</label>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</div>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'</td>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'</tr>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(document).on('click', '.btn_remove', function(){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var button_id = $(this).attr("id");&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$('#row'+button_id+'').remove();&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;});&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;</script>看法将 checkChoice 的初始值指定为 0,如下所示<div class="form-check">&nbsp;<input class="form-check-input" type="radio" name="checkChoice" id="checkChoice" value="0" checked&nbsp; />&nbsp; &nbsp; <label class="form-check-label" for="exampleRadios1">&nbsp; &nbsp; &nbsp; &nbsp; Make this as an Answer&nbsp; &nbsp; </label></div>// 插入答案$data_answer = $this->input->post('choice[]');$data_is_correct = $this->input->post('checkChoice');$value = array();for($i = 0; $i < count($data_answer); $i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($data_is_correct == $i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $correct = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $correct = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; $value[$i] = array(&nbsp; &nbsp; &nbsp; &nbsp; 'answer' => $data_answer[$i],&nbsp; &nbsp; &nbsp; &nbsp; 'correct' => $correct,&nbsp; &nbsp; &nbsp; &nbsp; 'question_id' => $this->db->insert_id(),&nbsp; &nbsp; );}
打开App,查看更多内容
随时随地看视频慕课网APP