我有一個問題,當我想插入其中使用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>
<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值
有人可以幫我嗎?
你爲什麼要加入$ i到每個變量的名字嗎?它不是一個數組,所以你會像$ this-> input-> post('check_os')[$ i]那樣訪問它嗎? – Silwerclaw
啊謝謝@Silwerclaw可憐的我,你可以說這是我第一次使用數組的東西。非常感謝您的幫助。 – codekiddies
只有一種形式,多行,你必須插入每行到db? –