문제

I am trying to insert a form that uses AJAX to initiate the controller. The form is generated from a search of another database which creates a looped (foreach) table. Users scan his/her badge into the input field and on tab or blur, the jQuery initiates the AJAX call to the controller which kicks in the model to insert into the db. It writes the data to the db, but it loops all rows of the table and inserts it into each row of the db. So I will have say 10 rows in the db with each column containing all ten rows of data for that column (from my implode I assume).

enter image description here

My AJAX call does return all values for each row.

Could someone look at my model and see where I have a second loop or something?

I have tried to just use a foreach loop in my model, I have tried various post methods.

Please be gentle this is my first time doing an insert_batch of an array from an looped view.

Thank you!!

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!';

        }


}

Model:

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;
    }

}

View:

        <?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>
도움이 되었습니까?

해결책

Yes it wont work because you are imploading same data again and again

Change your for loop like this

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);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top