php管理员有增删改权限,管理员增删改查

摘要:<?phpnamespace app\admin\controller;use app\admin\model\UserModel;use think\facade\Request;use app\admin\controller\Common;class User extends Common{    public function index()   &nb

namespace app\admin\controller;

use app\admin\model\UserModel;

use think\facade\Request;

use app\admin\controller\Common;

class User extends Common

{

public function index()

{

// 实例化模型

$user = new UserModel();

// 按id降序排列,并且每一页设为八条数据

$users = $user->order('id', 'desc')->paginate(8);

// 将数据赋值给模板

$this->view->users = $users;

// 渲染管理员界面

return $this->fetch();

}

public function add()

{

// 渲染管理员添加界面

return $this->fetch();

}

public function DoAdd()

{

// 获取前台提交过来的数据

$data = Request::param();

// 获取添加的时间

$data['time'] = time();

$username = $data['username'];

// 使用用户名来查询数据库是否有对应的数据

$res = UserModel::where('username', $username)->find();

// 判断数据是否存在

if ($res == true) {

return ['res' => 0, 'msg' => '用户名已存在!'];

}

// 实例化模型

$user = new UserModel();

// 验证数据是否存入数据库

if ($user->save($data)) {

return ['res' => 1, 'msg' => '添加成功!'];

} else {

return ['res' => 0, 'msg' => '添加失败!'];

}

}

public function edit()

{

// 获取前台提交过来的数据

$userId = Request::param('id');

// 通过用户id查询需要更新用户的所有数据

$user = UserModel::get($userId);

// 将数据赋值给模板

$this->view->user = $user;

// 渲染编辑页面

return $this->fetch();

}

public function DoEdit()

{

// 获取前台提交过来的数据

$data = Request::param();

// 实例化模型

$user = new UserModel();

// 对数据库中的数据进行修改更新

$res = $user->save([

'username' => $data['username'],

'time' => time(),

], ['id' => $data['id']]);

if ($res) {

return ['res' => 1, 'msg' => '修改成功!'];

}

}

public function del()

{

// 获取需要删除管理员的id

$userId = Request::param('id');

// 实例化模型

$user = new UserModel();

// 进行删除并验证操作

if ($user->destroy($userId)) {

// 返回提示信息

return['res'=>1,'msg'=>'删除成功!'];

}

}

}

批改老师:韦小宝批改时间:2018-12-29 09:05:49

老师总结:写的还是挺不错的,增删改查,熟悉的掌握了以后,面对很多小项目就很简单了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值