我的表格如下所示。
选择项目项目名称有一个下拉列表。一旦选择项目名称,然后输入其他字段并提交form.now我想将数据插入到具有项目ID的表中。 我的数据库结构如下。
id | project_id | item | description | qty | unit |率| l_cost | l_hrs |量
根据我的代码project_id仅插入第一行。这是我正在使用print_r
的数组数组([0] =>数组([project_id] => 1 [staff_id] => 2 [item_no] => 1 [描述] => 1 [数量] => 1 [单位] => [rate] => 1 [laboure_hrs] => 1 [laboure_cost] => 1 [金额] => 2)1 =>数组([project_id] => [ staff_id] => 2 [item_no] => 2 [description] => 2 [qty] => 2 [unit] => [rate] => 2 [laboure_hrs] => 2 [laboure_cost] => 2 [amount] => 8)[2] =>数组([project_id] => [staff_id] => 2 [item_no] => 3 [描述] => 3 [数量] => 3 [unit] => [rate] => 3 [laboure_hrs] => 3 [laboure_cost] => 3 [金额] => 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);
答案 0 :(得分:0)
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);
}
}