我是使用php CodeIgniter的新手。我可以使用php native插入数组。但是,请问如何使用CodeIgniter插入数组?帮帮我
我有一个用于很多复选框的程序。如果我检查了1个以上或检查了所有内容,则仍然插入1个数据。它说成功,但只有1个数据。所以我想插入多个。1.我该怎么做?请修复
这是我从控制器获得的功能指令代码:
public function order()
{
$this->form_validation->set_rules('id_sub', 'Id_sub', 'required|trim');
if ($this->form_validation->run() == false) {
$data['title'] = 'Order';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$data["home"] = $this->product_model->getAll_join();
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar', $data);
$this->load->view('templates/topbar', $data);
$this->load->view('user/order', $data);
$this->load->view('templates/footer');
} else {
$data = [
'id_sub' => htmlspecialchars($this->input->post('id_sub', true))
];
$this->db->insert('tbl_order_detail', $data);
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Data berhasil disimpan.
</div>');
redirect('user/order');
}
}
这是我的查看订单代码:
<tbody>
<tr>
<td>
<?php
$query = $this->db->query("select tbl_referensi.referensi, tbl_sub_kategori.sub, tbl_sub_kategori.id_sub
from tbl_referensi
inner join tbl_sub_kategori
on tbl_referensi.id_ref = tbl_sub_kategori.id_ref
where tbl_referensi.id_ref = $tampil->id_ref;
");
foreach ($query->result() as $tampil_sub) :
?>
<input type="checkbox" aria-label="Checkbox for following text input" name="id_sub" id="id_sub" value="<?= $tampil_sub->id_sub ?>"> <label for="name" class="mr-4"><?= $tampil_sub->sub ?></label>
<?php endforeach; ?>
</td>
</tr>
</tbody>
我认为问题出在功能顺序上,而标记HTML(name = {"id_sub"
)则来自视图顺序。也许使用id_sub []。
但是如何?请修复它并帮助我。
答案 0 :(得分:0)
使用此
<input type="checkbox" aria-label="Checkbox for following text input" name="id_sub[]" id="id_sub" value="<?= $tampil_sub->id_sub ?>"> <label for="name" class="mr-4"><?= $tampil_sub->sub ?></label>
您将在使用的控制器中获得价值
$post_data = $this->input->post('id_sub');
foreach ($post_data as $key=>$value) {
// do your code
}
答案 1 :(得分:0)
在html中,输入名称应为数组
<input type="checkbox" aria-label="Checkbox for following text input" name="id_sub[]" id="id_sub" value="<?= $tampil_sub->id_sub ?>"> <label for="name" class="mr-4"><?= $tampil_sub->sub ?>
Mysql查询:
$data = [
'id_sub' => implode(',',$this->input->post('id_sub'))
];
$this->db->insert('tbl_order_detail',$data);