主页 > 编程 > php >

php web通用权限管理之用户添加 设置角色代码

2019-07-16 21:21 阅读:146 来源:智宇SEO自媒体
php web通用权限管理之用户添加 设置权限代码
视图:
<div class="layui-form-item" pane="">
        <label class="layui-form-label">所属角色</label>
        <div class="layui-input-block">
                <?php foreach ($this->roleData as $r ) {?>
                        <input style="display: inline-block" type="checkbox" name="roleid[]" lay-skin="primary" title="<?php  echo $r['name'];?>" value="<?php echo $r['id']?>">
                        <?php echo $r[ 'name'];?>   
            <?php }?>
</div>
</div>

控制器:
public function add()
    {
        //角色
        $this->roledb=db('role');
        $this->roleData= $this->roledb->where('status=?', array(1))->fetchAll();


        if (!empty($_POST)) {
            $addData  = array('name' =>$_POST['name'],
                'pwd' =>$_POST['pwd'],
                'updated_time' =>date("Y-m-d H:i:s"),
                'created_time' =>date("Y-m-d H:i:s"),
            );
            if(empty($addData['name'])||empty($addData['pwd']))
            {
                e('用户不能为空');
            }
            else
            {
                $userId = $this->db->add($addData);
                if($userId){
                    if(isset($_POST['roleid']))
                    {

                        $roleid = $_POST['roleid'];
                        $rid =implode(',',$roleid);
                        $aid=explode(',',$rid);//从数据库取出数据时用。

                        $this->userroledb=db('user_role');

                        foreach($aid as $id)
                        {
                            $user_role_Data  = array('uid' =>$userId,
                                'role_id' =>$id,
                                'created_time' =>date("Y-m-d H:i:s"),
                            );
                            $user_role_id =$this->userroledb->add($user_role_Data);

                        }

                    }

                    s('user','index','用户添加成功!');

                }else{
                    e('用户添加失败');
                }
            }

        }else
        {
            $this->display('useradd.php');
        }

    }