gpt4 book ai didi

php - Codeigniter insert_batch for 循环问题

转载 作者:行者123 更新时间:2023-11-30 00:35:38 25 4
gpt4 key购买 nike

我正在尝试插入一个使用 AJAX 来启动 Controller 的表单。该表单是通过搜索另一个数据库生成的,该数据库创建了一个循环(foreach)表。用户将他/她的徽章扫描到输入字段中,然后在选项卡或模糊上,jQuery 启动对 Controller 的 AJAX 调用,该 Controller 启动模型以插入到数据库中。它将数据写入数据库,但它循环表的所有行并将其插入数据库的每一行。所以我会说数据库中有 10 行,每列包含该列的所有十行数据(我假设来 self 的内爆)。

enter image description here

我的 AJAX 调用确实返回每行的所有值。

有人可以看看我的模型并看看我在哪里有第二个循环或其他东西吗?

我尝试在模型中使用 foreach 循环,我尝试了各种 post 方法。

请温柔一点,这是我第一次从循环 View 中对数组进行 insert_batch。

谢谢!!

Controller :

    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>

最佳答案

是的,它不会工作,因为你一次又一次地导入相同的数据

像这样改变你的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);
}
}

关于php - Codeigniter insert_batch for 循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22202844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com