我想要的是,我有一个uniq id来电 num 我希望为那个唯一的 num 插入多个描述。我有一个表格来添加文件动态所以我可以添加我想要的字段。当我尝试插入一行数据时,当我尝试插入多个行数据时,它无法正常工作。
我的观看页面:
<form name="codexworld_frm" action="" method="post">
<div class="field_wrapper">
<input type="text" name="num" value=""/><br />
<input type="text" name="description[]" value=""/><input type="text" name="voucher_no[]" value=""/><input type="text" name="price[]" value=""/>
<a href="javascript:void(0);" class="add_button" title="Add field"><img src="<?php echo base_url('images/add-icon.png'); ?>"/></a>
</div>
<input type="submit" name="submit" value="SUBMIT"/>
</form>
我的控制器:
$data = array(
'no' => $this->input->post('num'),
'descriptions' => $this->input->post('description'),
'voucher' => $this->input->post('voucher_no'),
'des_price' => $this->input->post('price'),
);
$this->multi_model->form_insert($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('multi_view', $data);
我的模特:
function form_insert($data){
$this->db->insert('tbl_description', $data);
}
如果我在我的模型中使用foreache循环,我认为它会起作用,但我该如何使用?
当我使用print_r()功能时,这就是
1001
Array
(
[0] => description1
[1] => description2
[2] => description3
)
Array
(
[0] => voucher 1
[1] => voucher 2
[2] => voucher 3
)
Array
(
[0] => 100
[1] => 200
[2] => 300
)
答案 0 :(得分:5)
请参阅elastic4s链接它是使用多个插入而没有循环,或者您可以在控制器计数描述帖子中使用您的foreach循环并完成它。
ul{
background-color:lightgray;
padding:10px;
}
答案 1 :(得分:1)
you can insert object wise.
in your controller:-
$value=$this->input->post('num');
$count=count($val);
for($i=0; $i<$count; $i++){
$data['no']=$this->input->post('num')[$i];
$data['description']=$this->input->post('description')[$i];
$data['voucher']=$this->input->post('voucher')[$i];
$data['prize']=$this->input->post('prize')[$i];
$this->multi_model->form_insert($data);
}
答案 2 :(得分:0)
希望这会对你有所帮助..
控制器
//if voucher_no is required..
$voucher_no = $this->input->post('voucher_no');
$count = count($voucher_no);
if ($count > 0) {
for ($i = 0; $i < $count; $i++) {
if (!empty($voucher_no[$i])) {
$data = array(
'no' => $this->input->post('num'),
'descriptions' => $this->input->post('description')[$i],
'voucher' => $this->input->post('voucher_no')[$i],
'des_price' => $this->input->post('price')[$i],
);
$this->multi_model->form_insert($data);
}
}
}
$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('multi_view', $data);
让我们知道结果..
答案 3 :(得分:0)
更改模型如下。
function form_insert($data){
foreach($data['description'] as $key=>$des)
{
$savedata = array(
'no' => $data('no'),
'descriptions' => $des,
'voucher' => $data['voucher'][$key],
'des_price' => $data['dec_price'][$key],
);
$this->db->insert('tbl_description', $savedata);
}
}