标签:
html页面中运用了一个input内的数组 类似于这样的
<form name="form1" class="form-horizontal" action="<?php echo $this->config->item(‘base_url‘);?>/admincp/Goodsvideosaging/insert" enctype="multipart/form-data" method="post" >
<input type="hidden" name="kgId" id="kgId" value="<?php echo $kgInfo[‘kgId‘];?>" />
<div class="table-responsive clearfix" style="margin-top:40px">
<table class="table table-striped">
<thead>
<tr>
<th>商品名称</th>
<th>商品金额</th>
</tr>
</thead>
<tbody>
<?php if(!empty($month)) foreach($month as $k => $val){?>
<tr>
<td><input type="text" name="formArray[<?php echo $k;?>][goodsName]" id="goodsName" /></td>
<input type="hidden" name="formArray[<?php echo $k;?>][monthNum]" id="monthNum" value="<?php echo $val[‘monthNum‘]?>" />
<td><input type="text" name="formArray[<?php echo $k;?>][goodsMoney]" id="goodsMoney" />元</td>
</tr>
<?php }?>
<input type="hidden" name="allotFlag" id="allotFlag" value="1" />
</tbody>
</table>
<div class="form-group">
<div class="col-md-4 col-md-offset-1">
<input type="hidden" name="errormsg" id="errormsg" value="">
<button type="submit" class="btn btn-primary">提交</button>
</div>
</div>
</form>
php接收页面中
$formArray = $this->input->post(‘formArray‘); $kgId = intval($this->input->post(‘kgId‘)); $monthNum = array($this->input->post(‘monthNum‘)); $goodsName = array($this->input->post(‘goodsName‘)); $goodsMoney = array($this->input->post(‘goodsMoney‘)); $allotFlag = intval($this->input->post(‘allotFlag‘)); $KgArr = array(‘kgId‘=>$kgId); foreach($formArray as $k=>$v) { $newV = array_filter($v); if(isset($newV[‘goodsName‘]) && isset($newV[‘monthNum‘]) && isset($newV[‘goodsMoney‘])) { $newArray = array_merge($KgArr , $newV); $goodsId = $this->aging->addData($newArray); }
得到的formArray为一个数组。
是这种形式的
array(4) {
[0]=>
array(3) {
["goodsName"]=>
string(3) "111"
["monthNum"]=>
string(1) "1"
["goodsMoney"]=>
string(3) "111"
}
[1]=>
array(3) {
["goodsName"]=>
string(3) "222"
["monthNum"]=>
string(1) "3"
["goodsMoney"]=>
string(2) "22"
}
[2]=>
array(3) {
["goodsName"]=>
string(0) ""
["monthNum"]=>
string(1) "6"
["goodsMoney"]=>
string(0) ""
}
[3]=>
array(3) {
["goodsName"]=>
string(0) ""
["monthNum"]=>
string(2) "12"
["goodsMoney"]=>
string(0) ""
}
}
所以我们得先遍历这个数组,遍历之后。此时的$v仍然是一个一维数组此时按照CI的数据库添加数据来说,已经是没什么为题了。但是打印出来的数组中仍然有空数组,所以此时我们在这采用了
$newV = array_filter($v);
先去掉空的数组
由于在此时我需要跟别的数组一起合并入库,所以在此时我们运用到了数组合并的这样一个函数
$newArray = array_merge($KgArr , $newV);
然后在执行入库就可以了。
这种办法之前灭有用过,所以记下来了。当然,还有很多种批量添加的方法。大家可以别的地方找到。
这一点在CI手册的表单验证类里面有详解。附上地址 http://codeigniter.org.cn/user_guide/libraries/form_validation.html
标签:
原文地址:http://www.cnblogs.com/g825482785/p/CI.html