我想一键插入多行。我有一个名为add
的添加按钮。当我单击添加按钮时,它会加载另一行。在我的情况下,我提交一行,它保存到数据库并给出错误:
A PHP Error was encountered
Severity: Notice
Message: Uninitialized string offset: 0
Filename: controllers/boq_controller.php
Line Number: 46
当我点击添加按钮时也会发生这种情况。请帮我解决这些问题。
控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Boq_controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('boq_model');
}
public function index(){
$this->load->view('admin_include/header');
$this->load->view('project/boq');
}
public function create(){
if ($_POST)
{
$work_product_id=$this->input->post('work_product_id');
$work_item_description=$this->input->post('work_item_description');
$quentity=$this->input->post('quentity');
$rate=$this->input->post('rate');
$laboure_hrs=$this->input->post('laboure_hrs');
$laboure_cost=$this->input->post('laboure_cost');
$others=$this->input->post('others');
$txtmultTotal=$this->input->post('txtmultTotal');
$txtgrandTotal=$this->input->post('txtgrandTotal');
$data = array();
for ($i = 0; $i < count($this->input->post('work_product_id')); $i++)
{
$data[$i] = array(
'work_product_id' => $work_product_id[$i],
'work_item_description' => $work_item_description[$i],
'quentity' => $quentity[$i],
'rate' => $rate[$i],
'laboure_hrs' => $laboure_hrs[$i],
'laboure_cost' => $laboure_cost[$i],
'others' => $others[$i],
'txtmultTotal' => $txtmultTotal[$i],
'txtgrandTotal' => $txtgrandTotal[$i],
);
}
$this->boq_model->create($data);
}
}
}
模型
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Boq_model extends CI_Controller{
function create($data) {
$this->db->insert_batch('boq', $data);
}
}
?>
查看
<?php include 'header.php'; ?>
<div class="row clear_fix" style="width:1990px;">
<div class="col-md-12">
<div id="response"></div>
<div class="well">
<form class="form-inline" role="form" id="frmadd" action="<?php echo base_url() ?>index.php/boq_controller/create" method="POST">
<table class="table" id="boq_tbl">
<thead>
<th>Work Product Id</th>
<th>Work Item Description</th>
<th>Quentity</th>
<th>Unit</th>
<th>Rate</th>
<th>Laboure-Hrs</th>
<th>Labour Cost</th>
<th>Others</th>
<th>Total</th>
<thead>
<tbody>
<tr class="txtMult">
<td><input type="text" name="work_product_id" class="form-control" id="work_product_id" placeholder=""></td>
<td><input type="text" name="work_item_description" class="form-control" id="work_item_description" placeholder=""></td>
<td><input type="text" name="quentity" id="" class="form-control val1" /></td>
<td><select style=" height: 33px; width: 102px; border-radius: 2px;" >
<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"/></td>
<td><input type="text" name="laboure_hrs" id="" class="form-control val3" /></td>
<td><input type="text"name="laboure_cost" id="" class="form-control val4"/></td>
<td><input type="text" name="others" class="form-control" id="others" placeholder=""></td>
<td>
<span class="multTotal">0.00</span><input type="text" id="txtmultTotal" name="txtmultTotal">
<!-- <input type="text" class="multTotal" placeholder="0.00">-->
</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">ADD</button>
</p>
<div class="form-group">
<input type="submit" class="btn btn-success" id="btn btn-success" value="submit">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
$("#insert-more").click(function () {
$("#boq_tbl").each(function () {
var tds = '<tr class="txtMult">';
jQuery.each($('tr:last td', this), function () {
tds += '<td>' + $(this).html() + '</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
});
$("#boq_tbl").on("keyup", ".txtMult input", multInputs);
function multInputs() {
var mult = 0;
$("tr.txtMult").each(function () {
var val1 = $('.val1', this).val();
var val2 = $('.val2', this).val();
var val3 = $('.val3', this).val();
var val4 = $('.val4', this).val();
var total = (val1 * val2 ) + (val3 * val4);
$('.multTotal',this).text(total);
$('#txtmultTotal',this).val(total);
mult += total;
});
$("#grandTotal").text(mult);
$('#txtgrandTotal').val(mult);
}
</script>
</body>
</html>