美文网首页
php写批量删除

php写批量删除

作者: LauEl | 来源:发表于2020-05-07 09:58 被阅读0次

代码如下:

html
   <td class="am-text-middle">
                                        <input type="checkbox" name="sel" class="sel" value="<?= $item['file_id'] ?>">
                                    </td>


  <div class="tpl-table-black-operation">
                            <a href="javascript:void(0);" onclick="selectAll()">
                                全选
                            </a>
                            <a href="javascript:void(0);" onclick="converSelectAll()">
                                反选
                            </a>
                            <a href="javascript:void(0);" onclick="unSelectAll()">
                                全不选
                            </a>
                        </div>

js代码

<script type="text/javascript">
    var inputs =  document.getElementsByTagName("input");
    function selectAll(){
        for(var i = 0;i<inputs.length;i++){
            inputs[i].checked = true;
        }
    }
    function unSelectAll(){
        for(var i = 0;i<inputs.length;i++){
            inputs[i].checked = false;
        }
    }
    function converSelectAll(){
        for(var i = 0;i<inputs.length;i++){
            if(inputs[i].checked){
                inputs[i].checked = false;
            }else{
                inputs[i].checked = true;
            }
        }
    }
</script>
<script>
    $(function () {
        // 删除元素
        var url = "<?= url('content.files/recovery') ?>";
        $('.item-delete').delete('file_id', url, '确定要移入回收站吗?');
    });
</script>

<script>
    //批量删除
    $('#batchDelete').click(function () {
        var chk_value =[];
        $('input[class="sel"]:checked').each(function(){
            chk_value.push($(this).val());
        });
        if(chk_value.length==0){
            layer.msg('未勾选数据');
            return;
        }

        layer.confirm('您要批量直接删除吗?删除不可恢复!',
            {
                btn: ['确认','取消']
            }, function(){
            // alert(chk_value);
                $.ajax({
                    url:"<?= url('content.files/del_recycle') ?>"
                    ,type:'post'
                    ,data:{file_id:chk_value}
                    ,dataType : 'json'
                    ,success:function(res)
                    {
                        if(res.code == 1)
                        {
                            layer.alert(res.msg,{icon:1});
                            setTimeout(function(){
                                location.href = location.href;
                            },1500)
                        }else
                        {
                            layer.alert(res.msg,{icon:2});
                        }
                    }
                });
            });
    });
</script>
php代码
    /**
     * 批量直接删除
     * @param $file_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function del_recycle()
    {
        $file_id = input('post.file_id/a');
        $where = '';
        $where['file_id'] = array('in', $file_id);
        if (true == Db::table('yoshop_upload_file')->where($where)->update(['is_delete' => 1])) {
        $res = Db::table('yoshop_upload_file')->where($where)->select();
        foreach ($res as $k => $v) {
            $this->del_openid_file($_SERVER['DOCUMENT_ROOT'] . '/uploads' . '/' . $v['file_name']);
        }
            exit(json_encode(['code' => 1, 'msg' => '删除成功']));
        } else {
            exit(json_encode(['code' => 0, 'msg' => '删除失败']));
        }
    }

    /**
     * [del 文件删除]
     * @return [type] [description]
     */
    public function del_openid_file($filepath)
    {
        if(file_exists($filepath))
        {
            if(unlink($filepath))
            {
                return true;
            }
        }
        return false;
    }

相关文章

网友评论

      本文标题:php写批量删除

      本文链接:https://www.haomeiwen.com/subject/xydoghtx.html