テーブルと1つの特定のデータのフィールドに複数のデータをそのフィールドに異なる値を持つ挿入する方法はありますか?CodeIgniterの

夏冬 :

私は、クイズアプリを作っていますし、一つの質問が正しくなければなりません。私はすでに私が持っている問題は、私は他のものと異なっていなければならない一つの特定の選択や答えを持っている、である、私のテーブルで複数のデータを挿入することができます。

アプリのスクリーンショットと何イムがやろうとし

ここでは、画像の説明を入力します。

ここに私のテーブルは次のようになります。

table answers

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

1、正しい答えを示し、0が不正のために示しています。

だからここに私のモデルがあります。

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. Index 0 or the first data was the only data that can be inserted. 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

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 makeTomato or Grapes to be my answer or make it as value number 1 only Orange or the first added data.

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>
Antony Jack :

Dynamic Field Script

make variable val as a value of checkChoice like following...

  <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

<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>

//挿入回答

$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(),
    );
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=276196&siteId=1