我有查看提问者,我想插入多行(jawaban和soal),见下文
我的观点
<table>
<tr>
<td>1</td>
<td><input type="hidden" value="1" name="no_soal[]">apakah anda sehat</td>
<td><input type="checkbox" value="-5" name="jawaban[]"></td>
<td><input type="checkbox" value="-3" name="jawaban[]"></td>
<td><input type="checkbox" value="-1" name="jawaban[]"></td>
<td><input type="checkbox" value="0" name="jawaban[]"></td>
<td><input type="checkbox" value="1" name="jawaban[]"></td>
<td><input type="checkbox" value="3" name="jawaban[]"></td>
<td><input type="checkbox" value="5" name="jawaban[]"></td>
</tr>
<tr>
<td>10</td>
<td><input type="hidden" value="2" name="no_soal[]">apakah anda rajin mandi</td>
<td><input type="checkbox" value="-5" name="jawaban[]"></td>
<td><input type="checkbox" value="-3" name="jawaban[]"></td>
<td><input type="checkbox" value="-1" name="jawaban[]"></td>
<td><input type="checkbox" value="0" name="jawaban[]"></td>
<td><input type="checkbox" value="1" name="jawaban[]"></td>
<td><input type="checkbox" value="3" name="jawaban[]"></td>
<td><input type="checkbox" value="5" name="jawaban[]"></td>
</tr>
</table>
<input type="submit" class="submit btn btn-success" value="Submit"/>
我的控制器
public function add_soal4()
{
$id= $this->session->userdata('id');
$userid= $this->session->userdata('id');
$data = array();
if ($_POST)
{
$i = 0;
$no_soal = $this->input->post("no_soal");
$jawaban = $this->input->post("jawaban");
for ($i = 0; $i < count($this->input->post('id')); $i++)
{
$data[] = array(
'id' => $id,
'userid' => $userid,
'no_soal' => $no_soal[$i],
'jawaban' => $jawaban[$i]
);
}
$this->db->insert_batch('data_jawaban', $data);
}
}
现在我只能插入1条记录,我需要将所有选中的字段作为多个值插入数据库
答案 0 :(得分:0)
将insert
行放在for
循环中。
for ($i = 0; $i < count($this->input->post('id')); $i++) {
$data = array(
'id' => $id,
'userid' => $userid,
'no_soal' => $no_soal[$i],
'jawaban' => $jawaban[$i]);
$this->db->insert_batch('data_jawaban', $data);
}
答案 1 :(得分:0)
您应该将HTML
格式替换为如下所示
在视图中
<?=form_open();?>
<table>
<tr>
<td>1</td>
<td><input type="hidden" value="1" name="no_soal[1]">apakah anda sehat</td>
<td><input type="checkbox" value="-5" name="jawaban[1][]"></td>
<td><input type="checkbox" value="-3" name="jawaban[1][]"></td>
<td><input type="checkbox" value="-1" name="jawaban[1][]"></td>
<td><input type="checkbox" value="0" name="jawaban[1][]"></td>
<td><input type="checkbox" value="1" name="jawaban[1][]"></td>
<td><input type="checkbox" value="3" name="jawaban[1][]"></td>
<td><input type="checkbox" value="5" name="jawaban[1][]"></td>
</tr><tr>
<td>10</td>
<td><input type="hidden" value="2" name="no_soal[2]">apakah anda rajin mandi</td>
<td><input type="checkbox" value="-5" name="jawaban[2][]"></td>
<td><input type="checkbox" value="-3" name="jawaban[2][]"></td>
<td><input type="checkbox" value="-1" name="jawaban[2][]"></td>
<td><input type="checkbox" value="0" name="jawaban[2][]"></td>
<td><input type="checkbox" value="1" name="jawaban[2][]"></td>
<td><input type="checkbox" value="3" name="jawaban[2][]"></td>
<td><input type="checkbox" value="5" name="jawaban[2][]"></td>
</tr>
</table>
<input type="submit" class="submit btn btn-success" value="Submit"/>
<?= form_close();?>
通过以下代码替换您的功能
在控制器中
public function add_soal4(){
$id = $this->session->userdata('id');
$userid = $this->session->userdata('id');
if($this->input->post() && !empty($id) && !empty($userid)){
$no_soal_list = $this->input->post("no_soal");
$jawaban_list = $this->input->post("jawaban");
if(array_filter($jawaban_list)){
$answer_list = [];
foreach($jawaban_list as $no_soal => $jawaban_row){
foreach($jawaban_row as $jawaban){
$answer_list[] = compact('id','userid','no_soal','jawaban');
}
}
if(array_filter($answer_list)){
$this->db->insert_batch('data_jawaban', $answer_list);
echo ($this->db->trans_status()) ? 'Updated' : 'Failed';
}
}
}
}
答案 2 :(得分:0)
in&#34; view&#34;我添加自动插入脚本间隔10秒,但无法运行。这是我的脚本代码
<script type="text/javascript">
$(document).ready(function(){ autosave(); });
function autosave(){
var t = setTimeout("autosave()", 10000);
var id = $("#id").val();
var no_soal = $(this).find('input[name=no_soal]').eq(index).val();
var jawaban = $(this).find('input[name=jawaban]').eq(index).val();
$.ajax({
type: "POST",
url:"<?php echo base_url('online/add_soal4'); ?>",
dataType : "JSON",
data : {'id':id, 'no_soal':no_soal,'jawaban':jawaban},
cache: false,
}
});
}
</script>
答案 3 :(得分:0)
您将使用add_soal4
进行php提交和ajax调用,因此我们应该在add_soal4
函数中进行小修改
在 add_soal4()中的控制器
public function add_soal4(){
$id = $this->session->userdata('id');
$userid = $this->session->userdata('id');
if($this->input->post() && !empty($id) && !empty($userid)){
$no_soal_list = $this->input->post("no_soal");
$jawaban_list = $this->input->post("jawaban");
if($jawaban_list && array_filter($jawaban_list)){
$answer_list = [];
foreach($jawaban_list as $no_soal => $jawaban_row){
foreach($jawaban_row as $jawaban){
$answer_list[] = compact('id','userid','no_soal','jawaban');
}
}
if(array_filter($answer_list)){
$this->db->delete('data_jawaban', compact('id','userid'));
$this->db->insert_batch('data_jawaban', $answer_list);
echo ($this->db->trans_status()) ? 'Updated' : 'Failed';
}
}
}
if(!$this->input->is_ajax_request()){
// if you have any $this->load->view() file, call it here, dont call the view files outside the loop
}
}
在您的视图文件中添加此代码
<script type="text/javascript">
$(function(){
var autoinsert = setInterval(autosave, 10000); // 10 seconds
$('form').submit(function(e){
e.preventDefault();
clearInterval(autoinsert);
e.target.submit();
});
});
function autosave(){
$.ajax({
type: 'POST',
url: '<?= base_url('online/add_soal4'); ?>',
data: $('form').serialize(),
cache: false,
dataType: 'html',
success: function(data){
if(data){
console.log($.parseHTML(data)[0].textContent);
}
},
error: function(error){
console.log(error);
}
});
}
</script>