简体   繁体   中英

Codeigniter Insert Multiple Fields Using Foreach Loop

I have a problem when I want to insert multiple fields which are using foreach loop (to get the value form database), here is my form:

<form class="stdform" action="<?php echo $action_url; ?>" method="post">

    <label>Check Date</label> <input class="span3" type="text"
        id="datepicker" name="check_date" value="" required="required"
        placeholder="Tanggal cek" />

    <table class="table table-condensed table-hover table-bordered"
        style="font-size: 12;">
        <thead>
            <tr>
                <th class="head0">#</th>
                <th class="head0">Device</th>
                <th class="head0">IP Address</th>
                <th class="head0">NAT IP Address</th>
                <th class="head0">Check OS</th>
                <th class="head0">Application/Server</th>
                <th class="head0">Shift A</th>
                <th class="head0">Shift B</th>
                <th class="head0">Remark</th>
                <th class="head0">Report</th>
            </tr>
        </thead>
        <tbody>
            <!-- Row Template -->
            <?php 
                        $num=1;
                        foreach( $get_server_assets as $row ) {

                         ?>
            <tr>
                <td>
                    <?php echo $num; ?>
                </td>
                <td><input class="span12" type="text" name="device[]"
                    value="<?php echo $row->s_device; ?>" /></td>
                <td><input class="span9" type="text" name="ip_address[]"
                    value="<?php echo $row->s_ip; ?>" /></td>
                <td><input class="span9" type="text" name="nat_ip[]"
                    value="<?php echo $row->s_nat_ip; ?>" /></td>
                <td><select class="span12" name="check_os[]">
                        <option value=""></option>
                        <option value="Good">Good</option>
                        <option value="Not Good">Not Good</option>
                </select></td>
                <td><input class="span10" type="text" name="app_name[]"
                    value="<?php echo $row->app_server; ?>" /></td>
                <td><select class="span12" name="check_app_a[]">
                        <option value=""></option>
                        <option value="Good">Good</option>
                        <option value="Not Good">Not Good</option>
                </select></td>
                <td><select class="span12" name="check_app_b[]">
                        <option value=""></option>
                        <option value="Good">Good</option>
                        <option value="Not Good">Not Good</option>
                </select></td>
                <td><input class="span12" type="text" name="remark[]" value="" /></td>
                <td><input class="span12" type="text" name="report[]" value="" /></td>
            </tr>

            <?php $num++; } ?>

        </tbody>
    </table>

    <p class="stdformbutton">

        <button class="btn btn-primary btn-large" type="submit" name="submit"
            value="true">Submit</button>
        &nbsp; <a class="btn btn-large" onclick="window.history.back()">Cancel</a>

    </p>

</form>

And here is my controller:

$data = array();

        $count = count($this->input->post('device'));

        for($i=0; $i<$count; $i++) {

            $data[] = array(
                'check_date'=>$this->input->post('check_date'),
                'device'=>$this->input->post('device' . $i),
                'ip_address'=>$this->input->post('ip_address' . $i),
                'nat_ip'=>$this->input->post('nat_ip' . $i),
                'check_os'=>$this->input->post('check_os' . $i),
                'app_name'=>$this->input->post('app_name' . $i),
                'check_app_a'=>$this->input->post('check_app_a' . $i),
                'check_app_b'=>$this->input->post('check_app_b' . $i),
                'remark'=>$this->input->post('remark' . $i),
                'report'=>$this->input->post('report' . $i),
            );

        }

        $this->db->insert_batch('server_checklist',$data);

        /*foreach($_POST['data'] as $d) {

            $this->db->insert('server_checklist',$d);

        } */

        redirect('admin/viewServerChecklist');

When I submit there is no data insert, only check_date stored. And the rest give value 0.

Click here for image

Can someone help me??

try this, instead of appending, try to access the index of the array.

  $data[] = array(
            'check_date'=>$this->input->post('check_date'),
            'device'=>$this->input->post('device')[$i],
            'ip_address'=>$this->input->post('ip_address')[$i],
            'nat_ip'=>$this->input->post('nat_ip')[$i],
            'check_os'=>$this->input->post('check_os')[$i],
            'app_name'=>$this->input->post('app_name')[$i],
            'check_app_a'=>$this->input->post('check_app_a')[$i],
            'check_app_b'=>$this->input->post('check_app_b')[$i],
            'remark'=>$this->input->post('remark' )[$i],
            'report'=>$this->input->post('report')[$i],
        );

Try This:

$data_table = $this->input->post(NULL, TRUE);

        if (empty($data_table['device']))
        {
            redirect('admin/viewServerChecklist?message=error');
        }

        $required = array('device', 'ip_address', 'nat_ip', 'check_os', 'app_name', 'check_app_a', 'check_app_b', 'remark', 'report');
        $data = array();

        foreach ($data_table['device'] as $key => $value) 
        {
            // check list table. if value required not exist. continue to next index
            $row = array();
            foreach ($required as $key2 => $value2) 
            {
                if (!isset($data_table[$value2][$key])) continue;
                $row[$value2] = $data_table[$value2][$key];
            }
            // since check_date is not array. we need to parse to row data
            $row['check_date'] = $data_table['check_date'];
            $data[] = $row;
        }

        $this->db->insert_batch('server_checklist', $data);

        /*foreach($_POST['data'] as $d) {

            $this->db->insert('server_checklist',$d);

        } */

        redirect('admin/viewServerChecklist');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM