TP5 用户的页面操作

测试数据:

CREATE TABLE IF NOT EXISTS `tb_user` (
  `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `logintime` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

INSERT INTO `tb_user` (`id`, `name`, `password`, `logintime`) VALUES
(1, 'aaa', '123456', '2017-07-24 18:07:47'),
(2, 'bbb', '123456', '2017-07-24 18:03:22'),
(3, 'ccc', '123456', '2017-07-24 18:08:25');

控制器:

<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
class Index extends Controller
{
	protected $beforeActionList = [
       'gosession' =>  ['except'=>'login,login_all'], //tp前置方法,不管执行那个方法,都要先执行gosession , 除了login,login_all方法
    ];
    //定义前置控制器
    public function gosession()
    {   
        $id=Session::get('id');
    	if(!$id)
    	{
    		$this->error('请先登录','login');
    	}
    }
    //用户管理首页, 登录成功后的页面
    public function index()
    {
        $db = db('user');
    	$data = $db->select();
		return $this->fetch('index',['data'=>$data]);
    }
    //退出登录
    public function login_out()
    {
    	session::clear();
        $this->success('退出成功','login');
    }
    //登录页面
    public function login()
    {
    	return $this->fetch('login');
    }
    //用户登录方法
    public function login_all()
    {
    	$db = db('user');
    	$name = input('post.name');
     	$password = input('post.password');
        // 查询数据
		$list = $db->where(['name'=>$name,'password'=>$password])->find();
        //如果存在就存入session,并且跳转首页
		if($list)
		{	
			Session::set('name',$name);
			Session::set('id',$list['id']);
			$db->where(['name'=>$name,'password'=>$password])->update(['logintime'=>date("Y-m-d H:i:s")]);
			$this->redirect('index');
		}else{
			$this->error('登录失败','login');
		}
    }
    //用户添加页面
    public function add()
    {
    	return $this->fetch('add');
    }
    //用户添加方法
    public function add_all()
    {
        $name = input('post.name');
        $password = input('post.password');
        $db = db('user');
        //添加用户
        $list = $db->insert(['name' => $name, 'password' => $password]);
        //判断添加用户是否成功
        if($list){
            $this->success('添加成功!','index');
        }else{
            $this->error('添加错误!','index');
        }
    }
    //用户修改页面
    public function update($id)
    {     
        $db = db('user');
        $data = $db->where(['id'=>$id])->find();
    	return $this->fetch('update',['data'=>$data]);
    }
    //用户修改方法
    public function update_all()
    {
        $db = db('user');
        $name = input('name');
        $id = input('id');
        $password = input('password');
        $upd = $db->where(['id'=>$id])->update(['name'=>$name,'password'=>$password]);
        if($upd){
            echo '修改成功';
            $this->redirect('index');
        }else{
            echo '修改失败';
        }
    }
    //删除用户方法
    public function delete($id)
    {
        $db = db('user');
        $del=$db->where(['id' => $id])->delete();
        $this->redirect('index');
    }
}

视图->用户登陆:

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>用户登陆</title>
</head>
<body>
<h3>用户登陆</h3>
<form method="post" action="{:url('login_all')}">  
	<input type="text" required="required" placeholder="用户名" name="name"></input>  
	<input type="password" required="required" placeholder="密码" name="password"></input>  
	<button type="submit">登录</button>  
</form>
</body>
</html>

效果图:

 

视图->显示用户列表:

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>显示用户列表</title>
</head>
<body>
<h3>welcome:<font style=" color: red;">{$_SESSION['think']['name']} <a href="{:url('login_out')}">退出登录</a></font></h3>
<table>  
	<caption>用户管理</caption>   
	<tr>  
		<th>ID</th>
		<th>用户名 </th>
		<th>登录时间</th>
		<th>操作</th>
	</tr>
	{volist name="data" id="vo"}
	<tr>  
		<td>{$vo.id}</td>
		<td>{$vo.name}</td>
		<td>{$vo.logintime}</td>
		<td><a href="add">添加</a>|<a href="{:url('update')}?id={$vo.id}">修改</a>|<a href="{:url('delete')}?id={$vo.id}">删除</a></td>
	</tr>
	{/volist} 
</table> 
</body>
</html>

效果图:

 

视图->添加用户:

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<h3>添加用户</h3>
<form method="post" action="{:url('add_all')}">  
	<input type="text" required="required" placeholder="用户名" name="name"></input>  
	<input type="password" required="required" placeholder="密码" name="password"></input>  
	<button type="submit">提交</button>
</form>	
</body>
</html>

效果图:

 

视图->修改用户:

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>修改用户</title>
</head>
<body>
<h3>修改用户ID:{$data['id']}</h3>
<form method="post" action="update_all">  
	<input type="text" required="required" placeholder="用户名" name="name" value="{$data['name']}"></input>  
	<input type="password" required="required" placeholder="密码" name="password"></input>
	<input type="text" hidden="false" name="id" value="{$data['id']}">  
	<button type="submit">提交</button>  
</form>  
</body>
</html>

 效果图:

 

猜你喜欢

转载自onestopweb.iteye.com/blog/2386670
今日推荐