我正在尝试插入一个使用AJAX启动控制器的表单。该表单是通过搜索另一个创建循环(foreach)表的数据库生成的。用户将他/她的徽章扫描到输入字段和选项卡或模糊,jQuery启动对控制器的AJAX调用,控制器启动模型以插入数据库。它将数据写入db,但它循环表的所有行并将其插入到db的每一行。所以我会在数据库中说10行,每列包含该列的所有十行数据(我假设我的内容)。
我的AJAX调用会返回每行的所有值。
有人可以看看我的模型,看看我有第二个循环或其他什么东西吗?
我试图在我的模型中使用foreach循环,我尝试了各种post方法。
请保持温和,这是我第一次从循环视图中执行数组的insert_batch。
谢谢!
控制器:
function pp_traveler_job() {
if ($this -> ppullers_model -> AssignJob() == TRUE)
{
$this -> session -> set_flashdata('flashSuccess', '<br>Your form was successfully processed.');
redirect('/parts_pullers_traveler');
echo "Your data was inserted into the database!";
} else {
$this -> session -> set_flashdata('FlashFail', '<br>Your data did not entered.');
echo 'Your data was not inserted in the database!';
}
}
型号:
function AssignJob() {
$emp_id = $this -> input -> post('emp_id');
$job_number = $this -> input -> post('job_number');
$component_part_no = $this -> input -> post('component_part_no');
$component_desc = $this -> input -> post('component_desc');
$quantity = $this -> input -> post('quantity');
$uom = $this -> input -> post('uom');
$data = array();
$now = date("Y-m-d H:i:s");
for ($i = 0; $i < count($this -> input -> post('component_part_no')); $i++) {
if ($_POST['component_part_no'][$i] != '') {
$data[] = array(
'emp_id' => implode(',', $emp_id),
'job_number' => implode(',', $job_number),
'component_part_no' => implode(',', $component_part_no),
'component_desc' => implode(',', $component_desc),
'quantity' => implode(',', $quantity),
'uom' => implode(',', $uom),
'time_assigned' => $now);
}
}
$dataCount = count($data);
if ($dataCount) {
$this -> db -> insert_batch('trvlr_pp_data', $data);
}
if ($this -> db -> affected_rows() !== '') {
return TRUE;
return $dataCount;
}
}
查看:
<?php
$attributes = array('class' => 'pp_job_form', 'id' => 'pp_job_form');
echo form_open('/ppullers_controller/pp_traveler_job', $attributes);
?>
<p>
<label for="emp_id">Scan Badge: <span class="required">*</span></label>
<?php echo form_error('emp_id'); ?>
<br /><input id="emp_id" type="text" name="emp_id[]" class="empIdOnly" maxlength="10" value="<?php echo set_value('emp_id'); ?>" placeholder="Emp ID" autofocus />
<?php echo form_error('job_number'); ?><input id="job_number" type="hidden" name="job_number[]" maxlength="15" value="<?php foreach ($query as $jobno); echo substr($jobno -> fjobno, 0, -5); ?>" />
<div id="pp-table">
<table id="tfhover" class="tftable">
<tr>
<th>Comp Pt No.</th>
<th>BOM Desc</th>
<th>QTY</th>
<th>UoM</th>
<th>Component Scan</th>
<th>QTY Pulled</th>
</tr>
<?php foreach($query as $item):
?>
<tr>
<td>
<?php echo form_error('component_part_no'); ?>
<span>
<input id="component_part_no" type="text" name="component_part_no[]" size="10" maxlength="10" value="
<?= trim($item -> fbompart) ?>" readonly style="background-color: #CEE6F6; color: #202D64;"
</span>
</td>
<td>
<?php echo form_error('component_desc'); ?>
<span>
<input id="component_desc" type="text" name="component_desc[]" size="50" maxlength="225" value="
<?= $item -> fbomdesc ?>" readonly style="background-color: #CEE6F6; color: #202D64;"
</span>
</td>
<td>
<?php echo form_error('quantity'); ?>
<span>
<input id="quantity" type="text" name="quantity[]" size="4" maxlength="4" value="
<?= rtrim($item -> factqty, ".0") ?>" readonly style="background-color: #CEE6F6; color: #202D64;"
</span>
</td>
<td>
<?php echo form_error('uom'); ?>
<span>
<input id="uom" type="text" name="uom[]" size="2" maxlength="4" value="
<?= $item -> fbommeas ?>" readonly style="background-color: #CEE6F6; color: #202D64;"
</span>
</td>
<td>
<?php echo form_error('comp_scan'); ?>
<span>
<input id="comp_scan" type="text" name="comp_scan[]" size="20" maxlength="20" value="
<?php echo set_value('comp_scan'); ?>"
</span>
</td>
<td>
<?php echo form_error('qty_pulled'); ?>
<span>
<input id="qty_pulled" type="text" name="qty_pulled[]" size="10" maxlength="15" value="
<?php echo set_value('qty_pulled'); ?>"
</span>
</td>
</tr>
<?php endforeach; ?>
</table>
<div id="pp-submit">
<!-- form submit and reset buttons -->
<span style="width: 70px; margin-top: 25px; float: left;">
<!-- overrides css on the form -->
<?php
$data = array('name' => 'submitbtn', 'class' => 'submit', 'type' => 'submit', 'value' => 'Submit', 'id' => 'submit');
echo form_submit($data);
?>
</span>
<span style="width: 70px; margin-top: 25px; margin-bottom: 25px; float: left;">
<!-- overrides css on the form -->
<?php
$data = array('name' => 'reset2', 'class' => 'reset', 'type' => 'reset', 'value' => 'Reset', 'id' => 'reset2');
echo form_reset($data);
?>
</span>
<?php echo form_close(); ?>
</div>
</div>
答案 0 :(得分:0)
是的,因为你一次又一次地加载相同的数据而无法正常工作
像这样改变你的for循环
if(is_array($emp_id)){
foreach ($emp_id as $key => $_emp_id) {
$data[] = array(
'emp_id' => $_emp_id,
'job_number' => $job_number[$key],
'component_part_no' => $component_part_no[$key],
'component_desc' => $component_desc[$key],
'quantity' => $quantity[$key],
'uom' => $uom[$key],
'time_assigned' => $now);
}
}