PHP小白到大牛直播班+Linux服务器(前30名)

管理员管理总结

原创2018-12-26 20:58:19159
摘要:一管理员列表    管理员列表页面,就是查询出所有的admins表中的数据,然后将其渲染到视图中public function index() {     //加载管理员列表     $data = SysDb::table('ad

一管理员列表

    管理员列表页面,就是查询出所有的admins表中的数据,然后将其渲染到视图中

public function index()
{
    //加载管理员列表
    $data = SysDb::table('admins')->order('id desc')->lists();
    $this->view->assign('lists',$data);
    return $this->view->fetch();
}

10.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
    <script src="/static/plugins/layui/layui.js"></script>
    <style>
 .header span{ background: #FF5722;color:#fff;padding:10px;margin-left: 30px;line-height: 36px;}
        .header button{margin-right:20px;float:right;margin-top: 5px;}
        .header div{border-bottom: 2px solid #FF5722;}
    </style>
</head>
<body>
    <div class="header">
        <span>管理员列表</span>
        <button class="layui-btn-sm layui-btn" onclick="add()">添加</button>
        <div></div>
    </div>
    <table class="layui-table">
        <thead>
            <tr>
                <th>ID</th>
                <th>用户名</th>
                <th>真实姓名</th>
                <th>角色</th>
                <th>状态</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
 {volist name="lists" id="vo"}
            <tr>
                <td>{$vo.id}</td>
                <td>{$vo.username}</td>
                <td>{$vo.truename}</td>
                <td>{$vo.gid}</td>
                <td>{$vo.status==0 ? '正常' : '<span style="color:red">禁用</span>'}</td>
                <td>{$vo.add_time|date="Y-m-d H:i:s"}</td>
                <td>
                    <button class="layui-btn layui-btn-xs" onclick="add({$vo.id})">编辑</button>
                    <button class="layui-btn layui-btn-danger layui-btn-xs" onclick="del({$vo.id})">删除</button>
                </td>
            </tr>
 {/volist}
        </tbody>
    </table>
<script>
 //添加方法

 layui.use(['layer'],function () {
        layer = layui.layer;
 $ = layui.jquery;
 });
 function add(id){
        layer.open({
            type:2,
 title:id>0?'编辑管理员':'添加管理员',
 shade:0.3,
 area:['480px','400px'],
 content:'/index.php/admins/admin/add?id='+id,
 })
    }
    function del(id) {
        layer.confirm('确定要删除么?',{
            icon:3,
 btn:['确定','取消'],
 },function () {
            $.post('/index.php/admins/admin/del',{
                'id':id
            },function (data) {
                if(data.res == 1){
                    layer.alert(data.msg,{'icon':2})
                }else{
                    layer.alert(data.msg,{'icon':1})
                    setTimeout(function(){window.location.reload()},1500)
                }
            },'json')
        })
    }
</script>
</body>
</html>

二.新增管理员操作 /更新操作

    更新操作和新增操作公用一个方法,当点击新增按钮,不传入id,当点击编辑按钮,则将点击的数据的id传入到控制器方法中,在控制器中进行判断,如果有获取到id的值,则是更新操作,且根据id来查询数据表中数据,将其赋值给模板变量,如果没有id,则是新增操作,更新操作不可以编辑用户名,所以判断是否有id,如果有id则用户名表单不可编辑.将表单中的数据通过post方式传到控制器中,然后进行判断,各个字段的值是否为空,为空则返回相对信息,如果都满足,则执行添加/更新操作,其中密码字段在更新操作时不需要赋值给视图中,所以当更新时(id有值),如果密码有值就进行更新,没有值就不进行更新,所以只有当id没有值的时候才进行密码为空判断

    //添加管理员
    public function add()
    {
        $id = (int)input('get.id');
        $ids = SysDb::table('admins')->where(['id'=>$id])->item();
        $this->view->assign('ids',$ids);
        //渲染模板
        return $this->view->fetch();
    }

    //保存管理员
    public function save(){
    //获取表单提交的数据
        $data['username'] = trim(input('post.username'));
        $data['gid'] = (int)input('post.gid');
        $data['truename'] = input('post.truename');
        $data['status'] = (int)input('post.status');
        $data['id'] = (int)input('post.id');
        $password = input('post.password');
        
        if(!$data['username']){
            return ['res'=>1,'msg'=>'用户名不能为空'];
        }
        if(!$data['gid']){
            return ['res'=>2,'msg'=>'角色不能为空'];
        }
        if(!$data['truename']){
            return ['res'=>3,'msg'=>'真实姓名不能为空'];
        }
        //只有id等于0时也就是新增时才进行判断密码是否为空
        if($data['id'] == 0 && !$password){
            return ['res'=>5,'msg'=>'密码不能为空'];
        }
        //有密码,进行加密
        if($password){
            $data['password'] = md5($data['username'].$password);
        }
        $res = true;
        //如果id等于0,所以进行新增操作,
        if($data['id'] == 0){
            $item = SysDb::table('admins')->where(['username'=>$data['username']])->item();
            if($item){
//            exit(json_encode(array('res'=>4,'msg'=>'该用户已经存在')));
                return ['res'=>4,'msg'=>'该用户已经存在'];
            }
            $data['add_time'] = time();
            $res = SysDb::table('admins')->insert($data);
        }else{    //进行更新操作
            $res = SysDb::table('admins')->update($data);
        }
        //返回结果
        if($res){
            exit(json_encode(['res'=>0,'msg'=>'保存成功!']));
        }else{
            exit(json_encode(['res'=>6,'msg'=>'保存失败!']));
        }
    }


13.png12.png

12.png

三.删除操作

    删除操作,就是将id通过post传入然后进行强制转换,确保安全,然后进行删除操作

public function del(){
    $id = (int)input('post.id');
    $res = SysDb::table('admins')->where(['id'=>$id])->delete();
    if($res){
        exit(json_encode(['res'=>0,'msg'=>'删除成功!']));
    }
    exit(json_encode(['res'=>1,'msg'=>'删除失败!']));
}

14.png



批改老师:韦小宝批改时间:2018-12-27 09:27:54
老师总结:你的作业一直写的都很棒!写的很详细!继续加油吧!不要骄傲!

发布手记

热门词条