我的以下模型用于db中的插入数组。我还提交了视图文件示例。请帮助在codeigniter中使用循环插入批处理数据。
class Purchase_model extends CI_Model{
public function purchase(){
$price = $this->input->post('price');
$quantity = $this->input->post('quantity');
$date = $this->input->post('date');
$vendor_name = $this->input->post('vendor_name');
$model = $this->input->post('model');
$invoice_no = $this->input->post('invoice');
$temp = count($this->input->post('vendor_name'));
for($i=0; $i<$temp; $i++){
$data = array(
'date'=>$date[$i],
'vendor_name'=>$vendor_name[$i],
'model'=>$model[$i],
'price' =>$price[$i],
'purchase_quantity'=>$quantity[$i],
'amount' =>$price[$i]*$quantity[$i],
'invoice_no'=>$invoice_no[$i]
);
$insert = $this->db->insert('purchase',$data);
return $insert; }
}
但是当我提交时,我在db
中得到以下值 Sl date Vendor name model price quantity amount invoice
89 2 A 1 0 0 a
请帮忙。 我需要在db中使用一个表单插入多个值。
我的观看表格似乎
<?php
$data = array ('name' => 'quantity',
'class' =>'input-xlarge',
'value' => set_value('quantity')
);
?>
<?php echo form_input ($data); ?>
我的控制器是
public function purchase()
{
if($this->Purchase_model->purchase()){
$this->session->set_flashdata('Success', 'You are entered data successfully');
redirect('home/purchase_form');
}
}
答案 0 :(得分:1)
您应该使用insert_batch
功能,它应该解决您的问题,它更干净/更高效,因为它只需要一个查询:
class Purchase_model extends CI_Model {
public function purchase() {
$price = $this->input->post('price');
$quantity = $this->input->post('quantity');
$date = $this->input->post('date');
$vendor_name = $this->input->post('vendor_name');
$model = $this->input->post('model');
$invoice_no = $this->input->post('invoice');
$count = count($this->input->post('vendor_name'));
$insert_data = array();
for($i=0; $i < $count; $i++){
$data = array(
'date'=>$date[$i],
'vendor_name'=>$vendor_name[$i],
'model'=>$model[$i],
'price' =>$price[$i],
'purchase_quantity'=>$quantity[$i],
'amount' =>$price[$i]*$quantity[$i],
'invoice_no'=>$invoice_no[$i]
);
$insert_data[] = $data;
}
$insert = $this->db->insert_batch('purchase', $insert_data);
return $insert;
}
}
答案 1 :(得分:0)
//use text box as multiple you can try this... eg. 'name' => 'quantity[]'
<?php
$data = array ('name' => 'quantity[]',
'class' =>'input-xlarge',
'value' => set_value('quantity')
);
?>
<?php echo form_input ($data); ?>
答案 2 :(得分:0)
尝试以下代码
$data = array ('name' => 'price[]',
'class' =>'input-xlarge',
'value' => set_value('price')
);
echo form_input ($data);
$data = array ('name' => 'quantity[]',
'class' =>'input-xlarge',
'value' => set_value('quantity')
);
echo form_input ($data);
$data = array ('name' => 'date[]',
'class' =>'input-xlarge',
'value' => set_value('date')
);
echo form_input ($data);
$data = array ('name' => 'vendor_name[]',
'class' =>'input-xlarge',
'value' => set_value('vendor_name')
);
echo form_input ($data);
$data = array ('name' => 'model[]',
'class' =>'input-xlarge',
'value' => set_value('model')
);
echo form_input ($data);
$data = array ('name' => 'invoice[]',
'class' =>'input-xlarge',
'value' => set_value('invoice')
);
echo form_input ($data);
class Purchase_model extends CI_Model {
public function purchase() {
$data_array = $this->input->post();
$vendor_data = $data_array['vendor_name'];
foreach($vendor_data as $key => $value){
$amount = $data_array['price'][$key] * $data_array['quantity'][$key];
$data = array(
'date' => $data_array['date'][$key],
'vendor_name' => $data_array['vendor_name'][$key],
'model' => $data_array['model'][$key],
'price' => $data_array['price'][$key],
'purchase_quantity' => $data_array['quantity'][$key],
'amount' => $amount,
'invoice_no' => $data_array['invoice'][$key]
);
$insert_data[] = $data;
}
$insert = $this->db->insert_batch('purchase', $insert_data);
return $insert;
}
}