简体   繁体   English

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

[英]Is there any way to insert multiple data in a field of table and one specific data has different value on its field? Codeigniter

I am making a quiz app and one question must be correct.我正在制作一个测验应用程序,一个问题必须是正确的。 I can already insert multiple data in my table, the problem that i have is, I have a one specific choice or answer that must be different from the others.我已经可以在我的表中插入多个数据,我遇到的问题是,我有一个特定的选择或答案必须与其他选择或答案不同。

A screenshot of the app and what im trying to do该应用程序的屏幕截图以及我正在尝试做什么

在此处输入图片说明

Here's what my table looks like这是我的桌子的样子

table answers

id | answer | correct
 1 |  Apple |     1
 2 |  Mango |     0
 3 |  Melon |     0

1, indicate for correct answer and 0 indicate for incorrect. 1,表示正确答案,0表示不正确。

So here is my Model.所以这是我的模型。

I tried to make an attempt to fetch the radio button value which is the 1 value and insert it to the database but the result was, IF I add another data or multiple data.我试图尝试获取单选按钮值,即1并将其插入到数据库中,但结果是,如果我添加了另一个数据或多个数据。 Index 0 or the first data was the only data that can be inserted.索引 0 或第一个数据是唯一可以插入的数据。 I CANNOT choose what radio button i can check only the FIRST button I can check.不能选择什么单选按钮,我只能检查的第一个按钮,我可以检查。

// 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;
    }

Problem of my table if I add 3 new data如果我添加 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

I cannot make Tomato or Grapes to be my answer or make it as value number 1 only Orange or the first added data.我不能将TomatoGrapes作为我的答案,也不能仅将其作为值 1 的橙色或第一个添加的数据。

VIEW看法

So here is my radio button所以这是我的单选按钮

<div class="form-check">
 <input class="form-check-input" type="radio" name="checkChoice[]" id="checkChoice" value="1" checked  />
    <label class="form-check-label" for="exampleRadios1">
        Make this as an Answer
    </label>
</div>

And my form.还有我的表格。

<form method="post" id="myForm" action="<?php echo base_url(); ?>posts/addQuestion">
                    <div class="input-group">
                        <div class="input-group-prepend">
                            <span class="input-group-text">Question</span>
                        </div>
                        <input type="text" placeholder="Enter your Question" name="question" id="question" class="form-control" required />
                    </div>

                    <hr>
                    <h5>Create Choices: </h5>
                    <div class="input-group input-group-sm mb-3">
                    <div class="table-responsive">  
                               <table class="table table-bordered" id="dynamic_field">  
                                    <tr>  

                                         <td><input type="hidden" name="choiceHid[]" value="0" /></td> 
                                         <td><input type="text" name="choice[]" id="choice" placeholder="Enter your Choice" class="form-control" /> </td>  
                                         <td><button type="button" name="add" id="add" class="btn btn-success"><span class="iconify" data-icon="ant-design:plus-circle-outlined" data-inline="false"></span> Add Response </button></td>  
                                         <td>
                                            <div class="form-check">
                                                <input class="form-check-input" type="radio" name="checkChoice[]" id="checkChoice" value="1" checked  />
                                                    <label class="form-check-label" for="exampleRadios1">
                                                        Make this as an Answer
                                                    </label>
                                            </div>
                                        </td>
                                    </tr>  

                               </table>  
                          </div>  
                    </div>

                    <hr>

                    <input type="button" id="btnSave" class="btn btn-block btn-info" value="Submit" />

</form>

Script脚本

<script>

    $('#btnSave').click(function(){
            var url = $('#myForm').attr('action');
            var data = $('#myForm').serialize();
            //validate form

                $.ajax({
                    type: 'ajax',
                    method: 'post',
                    url: url,
                    data: data,
                    async: false,
                    dataType: 'json',
                    success: function(response){
                        if(response.success){
                            $('#myForm')[0].reset();
                            if(response.type=='add'){
                                var type = 'added'
                            }
                            swal("Success!", "You created a Question!", "success");

                        }else{
                            alert('Error');
                        }
                    },
                    error: function(){
                        alert('Could not add data');
                    }
                });
        });


</script>

Dynamic Field Script动态字段脚本

<script>  
 $(document).ready(function(){  
      var i=1;  
      $('#add').click(function(){  
           i++;  
           $('#dynamic_field').append(
               '<tr id="row'+i+'">'+
                 '<td><input type="hidden" name="choiceHid[]" value="0" /></td>'+
                 '<td><input type="text" name="choice[]" placeholder="Enter your Choice" class="form-control" /></td>'+
                 '<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>'+
                 '<td>'+
                        '<div class="form-check">'+
                            '<input class="form-check-input" type="radio" name="checkChoice[]" id="checkChoice" value="1" />'+
                                    '<label class="form-check-label" for="exampleRadios1">'+
                                        'Make this as an Answer'+
                                    '</label>'+
                        '</div>'+
                 '</td>'+
               '</tr>'
               );  

      });  

      $(document).on('click', '.btn_remove', function(){  
           var button_id = $(this).attr("id");   
           $('#row'+button_id+'').remove();  
      });  

 });  
 </script>

Dynamic Field Script动态字段脚本

make variable val as a value of checkChoice like following...将变量val作为 checkChoice 的值,如下所示...

  <script>  
     $(document).ready(function(){  
          var i=1;  
          $('#add').click(function(){  
               var val =i;
               i++;  
               $('#dynamic_field').append(
                   '<tr id="row'+i+'">'+
                     '<td><input type="hidden" name="choiceHid[]" value="0" /></td>'+
                     '<td><input type="text" name="choice[]" placeholder="Enter your Choice" class="form-control" /></td>'+
                     '<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>'+
                     '<td>'+
                            '<div class="form-check">'+
                                '<input class="form-check-input" type="radio" name="checkChoice" id="checkChoice" value="'+val+'" />'+
                                        '<label class="form-check-label" for="exampleRadios1">'+
                                            'Make this as an Answer'+
                                        '</label>'+
                            '</div>'+
                     '</td>'+
                   '</tr>'
                   );  

          });  

          $(document).on('click', '.btn_remove', function(){  
               var button_id = $(this).attr("id");   
               $('#row'+button_id+'').remove();  
          });  

     });  
     </script>

View看法

asign initial value of checkChoice as 0 like following将 checkChoice 的初始值指定为 0,如下所示

<div class="form-check">
 <input class="form-check-input" type="radio" name="checkChoice" id="checkChoice" value="0" checked  />
    <label class="form-check-label" for="exampleRadios1">
        Make this as an Answer
    </label>
</div>

// 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++) {

          if($data_is_correct == $i) {
            $correct = 1;
          } else {
            $correct = 0;
          }
    $value[$i] = array(
        'answer' => $data_answer[$i],
        'correct' => $correct,
        'question_id' => $this->db->insert_id(),
    );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Dexie,我可以获取表中的所有对象,其中数组字段具有特定值作为其元素之一吗? - With Dexie, can I get all objects in a table where an array field has a specific value as one of its elements? 有没有办法从表中选择所有字段数据,除了使用nodejs的一个字段数据 - is there any way to select all the fields data from the table except one field data using nodejs 有什么办法从表中获取特定字段的值? - is there any way to get a specific field's value from a table? 检查是否有任何对象具有特定字段的值 - Check if any object has given value for specific field JSON数据已将字段名称连接到值字段(Django) - JSON data has concatenated field name into value field (Django) 有没有办法在Javascript中通过lodash按多个字段对数据进行分组 - Is there a way to groupBy data by multiple field by lodash in Javascript 根据数据字段值的样式表行 - Style table row according to data field value Codeigniter:如果未从控制器返回数据,则为文本输入字段的默认值 - Codeigniter: The default value of a text input field if data is not returned from the controller 在为一个输入字段选择一个值后,自动将相应的数据填充到不同的输入字段中 - auto populate corresponding data into different input fields after selecting a value to one input field javascript 在网页的“日期”字段中插入数据 - Insert data in Date Field of Webpage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM