问题描述
我有一个如下所示的表单。
i have a form as like below.
选择项目项目名称有一个下拉列表。一旦选择项目名称,然后输入其他字段并提交form.now我想插入数据到具有项目id的表。
我的数据库结构如下。
there is one dropdown for select project project name. once select project name and then enter other field and submit form.now i want to insert data into table with project id.my db structure as below.
id | project_id | item | description | qty | unit |速率| l_cost | l_hrs | amount
id | project_id | item |description |qty |unit| rate| l_cost|l_hrs|amount
根据我的代码project_id仅插入第一行。这是我使用的数组print_r
according to my code project_id insert only for first row. this is the array which i'm getting using print_r
Array([0] => Array([project_id] => 1 [staff_id] => 2 [item_no] => 1 [description] => 1 [qty] => 1 [unit] => [rate] => 1 [laboure_hrs] => 1 [laboure_cost] => 1 [amount] => 2) => Array([project_id] => [staff_id] => 2 [item_no] => 2 [描述] => 2 [qty] => 2 [unit] => [rate] => 2 [laboure_hrs] => 2 [laboure_cost] => 2 [amount] => 8) ] => [staff_id] => 2 [item_no] => 3 [description] => 3 [qty] => 3 [unit] => [rate] => 3 [laboure_hrs] => 3 [laboure_cost] => 3 [amount] => 18))
Array ( [0] => Array ( [project_id] => 1 [staff_id] => 2 [item_no] => 1 [description] => 1 [qty] => 1 [unit] => [rate] => 1 [laboure_hrs] => 1 [laboure_cost] => 1 [amount] => 2 ) 1 => Array ( [project_id] => [staff_id] => 2 [item_no] => 2 [description] => 2 [qty] => 2 [unit] => [rate] => 2 [laboure_hrs] => 2 [laboure_cost] => 2 [amount] => 8 ) [2] => Array ( [project_id] => [staff_id] => 2 [item_no] => 3 [description] => 3 [qty] => 3 [unit] => [rate] => 3 [laboure_hrs] => 3 [laboure_cost] => 3 [amount] => 18 ) )
我的代码如下
查看
<form class="form-inline" role="form" id="frmadd" action="<?php echo base_url() ?>index.php/boq_controller/create" method="POST">
<!--project-->
<span><b>Project Name</b></span>
<?php
$attributes = 'class = "form-control" id = "project" style="width:100%; height:35px;"';
echo form_dropdown('project[]',$project, set_value('project'), $attributes);?>
<!--project-->
<!-- boq_tbl // field list-->
<table class="table" id="boq_tbl">
<tbody>
<!--<form class="form-inline" role="form" id="frmadd" action="<?php echo base_url() ?>index.php/boq_controller/create" method="POST">-->
<tr class="txtMult">
<td><input type="text" name="work_product_id[]" class="form-control" id="work_product_id" placeholder="" style="width:60px;" ></td>
<td><input type="text" name="work_item_description[]" class="form-control" id="work_item_description" placeholder="" style="width:350px;"></td>
<td><input type="text" name="quantity[]" id="" class="form-control val1" style="width:80px;" /></td>
<td><select style=" height: 28px; width: 70px; border-radius: 2px;" name="unit[]" >
<option value="" selected> </option>
<option value="cube">cube</option>
<option value="sq.ft">sq.ft</option>
<option value="Cwts">Cwts</option>
<option value="Gal" >Gal</option>
</select></td>
<td><input type="text" name="rate[]" class="form-control val2" style="width:80px;"/></td>
<td><input type="text" name="laboure_hrs[]" id="" class="form-control val3" style="width:80px;"/></td>
<td><input type="text"name="laboure_cost[]" id="" class="form-control val4" style="width:80px;"/></td>
<td>
<input type="text" id="txtmultTotal" name="txtmultTotal[]" style="width:80px;" placeholder="0.00" class="multTotal">
<!-- <input type="text" class="multTotal" placeholder="0.00">-->
</td>
<td><?php
//hidden value user session id
echo form_hidden('staff_id[]',($this->session->userdata['logged_in']['id']) );
?> </td>
</tr>
</tbody>
</table>
<p align="right">
Grand Total# <span id="grandTotal">0.00</span> <input type="text" id="txtgrandTotal" name="txtgrandTotal">
<button id="insert-more" class="btn btn-primary" type="button">ADD</button>
</p>
<div class="form-group">
<input type="submit" class="btn btn-success" id="btn btn-success" value="submit">
</div>
</form>
控制器
public function create(){
if ($_POST)
{
$project_id=$this->input->post('project');
$staff_id=$this->input->post('staff_id');
$item_no=$this->input->post('work_product_id');
$description=$this->input->post('work_item_description');
$qty=$this->input->post('quantity');
$unit=$this->input->post('unit');
$rate=$this->input->post('rate');
$laboure_hrs=$this->input->post('laboure_hrs');
$laboure_cost=$this->input->post('laboure_cost');
$amount=$this->input->post('txtmultTotal');
$data = array();
for ($i = 0; $i < count($this->input->post('work_product_id')); $i++)
{
$data[$i] = array(
'project_id' => $project_id[$i],
'staff_id' => $staff_id[$i],
'item_no' => $item_no[$i],
'description' => $description[$i],
'qty' => $qty[$i],
'unit' => $unit[$i],
'rate' => $rate[$i],
'laboure_hrs' => $laboure_hrs[$i],
'laboure_cost' => $laboure_cost[$i],
'amount' => $amount[$i],
);
}
$this->boq_model->create($data);
}
模型
function create($data) {
$this->db->insert('boq', $data);
推荐答案
public function create() {
if($_POST) {
$project_id = $this->input->post('project');
$staff_id = $this->input->post('staff_id');
$description = $this->input->post('work_item_description');
$qty = $this->input->post('quantity');
$unit = $this->input->post('unit');
$rate = $this->input->post('rate');
$laboure_hrs = $this->input->post('laboure_hrs');
$laboure_cost = $this->input->post('laboure_cost');
$amount = $this->input->post('txtmultTotal');
$data = array();
$item_no = $this->input->post('work_product_id');
foreach($item_no as $key => $value) {
$data[$key] = array(
'project_id' => $project_id,
'staff_id' => $staff_id[$key],
'item_no' => $value,
'description' => $description[$key],
'qty' => $qty[$key],
'unit' => $unit[$key],
'rate' => $rate[$key],
'laboure_hrs' => $laboure_hrs[$key],
'laboure_cost'=> $laboure_cost[$key],
'amount' => $amount[$key],
);
}
$this->boq_model->create($data);
}
}
这篇关于在表单中使用一个forigenk值插入多个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!