![](/img/trans.png)
[英]With Dexie, can I get all objects in a table where an array field has a specific value as one of its elements?
[英]Is there any way to insert multiple data in a field of table and one specific data has different value on its field? Codeigniter
我正在制作一個測驗應用程序,一個問題必須是正確的。 我已經可以在我的表中插入多個數據,我遇到的問題是,我有一個特定的選擇或答案必須與其他選擇或答案不同。
該應用程序的屏幕截圖以及我正在嘗試做什么
這是我的桌子的樣子
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 的橙色或第一個添加的數據。
看法
所以這是我的單選按鈕
<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>
還有我的表格。
<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>
$('#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>
動態字段腳本
<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>
動態字段腳本
將變量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>
看法
將 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>
// 插入答案
$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.