这是一个thinkphp中的批量修改的案例:
如需要删除多项,或者同时修改多项记录
要点:
前端表单中name要加[],如:<input type="hidden" name="id[]" value="{$vo.id}">
后端接受如:$id = $_POST [id] [$i]; $i为for中的循环变量,当$i相同时可以使得$_POST [id] [$i]和$_POST [name] [$i]为一一对应;
前端表单:
<form name="form1" method="post" action="__GROUP__/Repair/updateList"> 对所选操作:<input type="submit" value="保 存" class="btn btn-primary"> <br> <table id="checkList" class="list table table-hover"> <tr> <th scope="col"><input id="check" type="checkbox" onclick="checkAll()" /> 全选</th> <th scope="col">主题</th> <th scope="col">校区</th> <th scope="col">房间</th> <th scope="col">报修人</th> <th scope="col">联系电话</th> <th scope="col">报修时间</th> <th scope="col">审核</th> <th scope="col">报修状态</th> </tr> <volist name="list" id="vo"> <tr> <td><input type="checkbox" name="key" value="{$vo.id}">{$vo.id}<input type="hidden" name="id[]" value="{$vo.id}"></td> <td>{$vo.topic}</td> <td>{$vo.xiaoqu}</td> <td>{$vo.room}</td> <td>{$vo.pname}</td> <td>{$vo.phone}</td> <td>{$vo.create_time|date='Y-m-d H:i:s',###}</td> <td><select name="check[]"><switch name="vo.check"> <case value="0"> <option value="0" selected="selected">未审核</option> <option value="1">审核</option> </case> <default /> <option value="0">未审核</option> <option value="1" selected="selected">审核</option> </switch></select></td> <td><select name="status[]"><switch name="vo.status"> <case value="已修"> <option value="未修">未修</option> <option value="任务已下">任务已下</option> <option value="已修" selected="selected">已修</option> </case> <case value="任务已下"> <option value="未修">未修</option> <option value="任务已下" selected="selected">任务已下</option> <option value="已修">已修</option> </case> <default /> <option value="未修" selected="selected">未修</option> <option value="任务已下">任务已下</option> <option value="已修">已修</option> </switch></select></td> </tr> </volist> </table> </form>
服务端处理:
/** * 批量修改状态 */ public function updateList() { $result = false; for($i = 0; $i < count ( $_POST [id] ); $i ++) { $id = $_POST [id] [$i]; $check = $_POST [check] [$i]; $status = $_POST [status] [$i]; // 保存登录信息 $Repair = M ( 'Repair' ); $data ['id'] = $id; $data ['check'] = $check; $data ['status'] = $status; $result = $Repair->save ( $data ); } if (false !== $result) { $this->success ( '修改成功!' ); } else { $this->error ( '修改失败!' ); } }