第一种批量添加(不推荐,了解即可)
注:本文 使用PHP的 CI框架进行操作的。如有不明,请参考CI手册
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);
然后在执行入库就可以了。
这种办法之前灭有用过,所以记下来了。当然,还有很多种批量添加的方法。大家可以别的地方找到。
之后在公司一位大牛那又了解了这种批量添加。相对的来说这个还是比较方便的
第二种批量添加
页面中 js代码 一起加进来
<script type="text/javascript" src="<?php echo $this->config->item('base_url');?>/public/js/jquery-1.7.2.min.js"></script> <script> function submitForm() { var res = ''; $.ajax({ type:'post', url:'<?php echo $this->config->item('base_url');?>Show/add', dateType:'json', data:$('#formArray').serialize(), success:function(data) { res = data; } }) if(res.err == 0) { warning('<strong>成功</strong> '+res.msg); } else { warning('<strong>错误</strong> '+res.msg); } } function warning(a) { $('#warning').html(a); $('#warningerr').show(); } </script> <body> <div class="alert alert-warning alert-dismissible" role="alert" id="warningerr" style="display: none;"> <div id="warning"><strong>错误:</strong> 角色名重复</div> </div> <form id="formArray" name="formArray" method="post"> <input type="hidden" name="kgId" id="kgId" value="<?php echo $kgInfo['kgId']?>"> <table style=" %50; height: %50; border:1px solid"> <tr> <td>GoodsName:</td> <td>GoodsMoney:</td> </tr> <?php if(!empty($arr)) foreach($arr as $k=>$v){?> <tr> <td><input type="text" name="goodsName[]" id="goodsName" value="<?php echo $v['goodsName']?>"> </td> <td><input type="text" name="goodsMoney[]" id="goodsMoney"></td> <input type="hidden" name="monthNum[]" id="monthNum" value="<?php echo $v['monthNum']?>" /> </tr> <?php }?> </table> <button name="button" type="button" id="button" onclick="submitForm()">CLICK ME !</button> </form>
后台程序
$res = array('err'=>1 , 'msg'=> '参数错误'); $goodsName = $this->input->post('goodsName'); $goodsMoney = $this->input->post('goodsMoney'); $monthNum = $this->input->post('monthNum'); $kgId = $this->input->post('kgId'); $param = array(); if (!empty($goodsName)) { foreach($goodsName as $k=>$v) { $param = array(); $param['goodsName'] = $goodsName[$k] ? $goodsName[$k] : ''; $param['goodsMoney'] = $goodsMoney[$k] ? $goodsMoney[$k] : 0.00; $param['monthNum'] = $monthNum[$k] ? $monthNum[$k] : 0; $param['kgId'] = $kgId; $lastId = $this->showmodel->addData($param); if(!empty($lastId)) { $res['err'] = 0; $res['msg'] = '添加成功!'; } } } echo json_encode($res);
使用数组作为域名称
这一点在CI手册的表单验证类里面有详解。附上地址 http://codeigniter.org.cn/user_guide/libraries/form_validation.html