Codeigniter Вставка нескольких полей с помощью цикла Foreach

У меня проблема, когда я хочу вставить несколько полей, которые используют цикл foreach (чтобы получить базу данных формы значений), вот моя форма:

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

А вот и мой контроллер:

$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');

Когда я отправляю, данные не вставляются, сохраняется только check_date. А остальные дают значение 0.

Нажмите здесь для изображения

Кто-нибудь может мне помочь??

0

Решение

Попробуй это:

$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');
0

Другие решения

попробуйте это, вместо добавления, попробуйте получить доступ к индексу массива.

  $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],
);
0

По вопросам рекламы [email protected]