public function index()
{
$name = $this->request->get('name', "");
$password = $this->request->get('password', '');
$phone = $this->request->get('phone', '');
$where = [];
if (!empty($name)) {
array_push($where, [
'name',
'like',
"%$name%"
]);
}
if (!empty($password)) {
array_push($where, [
'password',
'=',
$password
]);
}
if (!empty($phone)) {
array_push($where, [
'phone',
'=',
$phone
]);
}
$model = new peop();
$result = $model->where($where)->paginate([
'query' => Request::param(), //url额外参数
'list_rows' => 5, //每页数量
]);
//定义一个变量来储存$result->toArray()
$page = $result->toArray();
//首页
$per_page = 1;
//上一页
$row_page = $page['current_page'] - 1;
if ($row_page <= $per_page) {
$row_page = 1;
}
//下一页
$After_page = $page['current_page'] + 1;
if ($After_page >= $page['last_page']) {
$After_page = $page['last_page'];
}
//最后一页 末页
$last_page = $page['last_page'];
return View::fetch('index', [
'list' => $result->toArray(),
'name' => $name,
'password' => $password,
'phone' => $phone,
'per_page' => $per_page,
'row_page' => $row_page,
'After_page' => $After_page,
'last_page' => $last_page
]);
}
public function add()
{
if ($this->request->isGet()) {
return View::fetch('add');
} else {
$data = $this->request->post();
if (empty($data['name'])) {
echo '用户名为空';
die;
}
if (empty($data['password'])) {
echo '密码为空';
die;
}
if (empty($data['phone'])) {
echo '手机号码为空';
die;
}
if ($data['sex'] != 0 && $data['sex'] != 1) {
echo '没有选择性别';
die;
}
$data['date_time'] = date('Y-m-d H:i:s');
$data['update_time'] = date('Y-m-d H:i:s');
$model = new peop();
if ($model->save($data)) {
echo '新增成功 <a href="/admins/index">用户列表<a/>';
} else {
echo '新增失败';
}
}
}
public function edit()
{
$id = $this->request->get('id');
if (empty($id)) {
echo 'id值有误';
die;
}
$model = new peop();
$result = $model->find($id);
if (empty($result)) {
echo '找不到该记录';
die;
}
$oldtime = strtotime($result['date_time']);
$result['date_time'] = date('Y-m-d', $oldtime);
// dump($result->toArray());die;
if ($this->request->isGet()) {
return View::fetch('edit', [
'result' => $result->toArray()
]);
} else {
$result['name'] = $this->request->post('name');
$result['password'] = $this->request->post('password');
$result['phone'] = $this->request->post('phone');
$result['sex'] = $this->request->post('sex');
if (empty($result['name'])){
echo '用户名为空';
die;
}
if (empty($result['password'])){
echo '密码为空';
die;
}
if (empty($result['phone'])){
echo '手机号码为空';
die;
}if (empty($result['sex']!==0&&$result['sex']!==1)){
echo '性别未选择';
die;
}
if ($result->save()){
echo '修改成功 <a href="/admins/index">用户列表</a>';
}
else{
echo '修改失败 <a href="/admins/index">用户列表</a>';
}
}
}
public function del()
{
$id = $this->request->get('id');
$model = new peop();
$result = $model->where([
['id', '=', $id]
])->find();
if ($result->delete()){
return $this->success('删除成功','/admins/index','','3');
// echo '删除成功 <a href="/admins/index">用户列表</a>';
}
else{
echo '删除失败 <a href="/admins/index">用户列表</a>';
}
}
public function login(){
if ($this->request->isGet())
{
return View::fetch('login');
}
else{
$name=$this->request->post('name');
$password=$this->request->post('password');
if (empty($name)){
return $this->error('用户名为空','/admins/login','');
}
if (empty($password)){
return $this->error('密码为空','/admins/login','');
}
$model = new peop();
$where=[];
array_push($where,[
'name',
'=',
$name
]);
$result = $model->where($where)->find();
if (empty($result)){
return $this->error('用户名或密码错误','/admins/login','');
}
if ($result['password']!=$password){
return $this->error('用户名或密码错误','/admins/login','');
}
return $this->success('登录成功','/admins/index','','0');
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
</style>
<script src="/static/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="div1">
<form action="" method="post">
用户名:<input type="text" name="name" id="name" value="" />
<br>
密码:<input type="text" name="password" id="password" value="" />
<br>
<button type="submit">登录</button>
</form>
</div>
</body>
<script>
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
#div1{
display: flex;
justify-content: center;
flex-direction: column;
}
#div1 td{
text-align: center;
}
#div2 a{
text-align: right;
}
#div2 button{
text-align: left;
}
</style>
<script src="/static/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="div1">
<form action="" method="get">
用户名:<input type="text" name="name" value="<?=$name?>" />
密码:<input type="text" name="password" value="<?=$password?>" />
手机号码:<input type="text" name="phone" value="<?=$phone?>" />
开户时间:<input type="date" name="" value="" />
<button type="submit">查询</button>
</form>
<br>
<p><a href="/admins/add">新增用户</a></p>
<br>
<table border="" cellspacing="1px" cellpadding="10px">
<tr>
<th><input type="checkbox" id="checkAll" /></th>
<th>用户名</th>
<th>密码</th>
<th>性别</th>
<th>手机号码</th>
<th>开户时间</th>
<th>状态</th>
<th>操作</th>
</tr>
<?php forEach($list['data'] as $item){?>
<tr>
<td><input type="checkbox" class="checks" value="<?=$item['id']?>" /></td>
<td><?=$item['name']?></td>
<td><?=$item['password']?></td>
<td><?=$item['sex']?'男':'女'?></td>
<td><?=$item['phone']?></td>
<td><?=$item['date_time']?></td>
<td style="width: 100px"><?=$item['stat']?"在线":"不在线"?></td>
<td>
<a href="/admins/edit?id=<?=$item['id']?>" >修改</a>
<a href="/admins/del?id=<?=$item['id']?>" >删除</a>
</td>
</tr>
<?php }?>
</table>
<p><button id="deleteAll" type="submit">删除选中</button> </p>
<p style="text-align: right">
<a href="/admins/index?name=<?=$name?>&password=<?=$password?>&phone=<?=$phone?>&page=<?=$per_page?>">首页</a>
<a href="/admins/index?name=<?=$name?>&password=<?=$password?>&phone=<?=$phone?>&page=<?=$row_page?>">上一页</a>
<a href="/admins/index?name=<?=$name?>&password=<?=$password?>&phone=<?=$phone?>&page=<?=$After_page?>">下一页</a>
<a href="/admins/index?name=<?=$name?>&password=<?=$password?>&phone=<?=$phone?>&page=<?=$last_page?>">末页</a>
</p>
</div>
</body>
<script>
$('#checkAll').on('change',function(){
$('.checks').prop('checked',this.checked)
});
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
用户名:<input type="text" name="name" >
<br>
密码:<input type="text" name="password">
<br>
手机号码:<input type="text" name="phone">
<br>
性别 :
<select name="sex">
<option value ="0">女</option>
<option value ="1">男</option>
</select>
<br>
<button type="submit">保存</button>
<a href="/admins/index">用户列表</a>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
用户名:<input type="text" name="name" value="<?=$result['name']?>">
<br>
密码:<input type="text" name="password" value="<?=$result['password']?>">
<br>
手机号码:<input type="text" name="phone" value="<?=$result['phone']?>">
<br>
开户时间:<input type="date" name="time" value="<?=$result['date_time']?>">
<br>
性别 :
<select name="sex">
<option value="0" <?=$result['sex']==0?'selected' :''?>>女</option>
<option value="1" <?=$result['sex']==1?'selected' :''?>>男</option>
</select>
<br>
<button type="submit">修改</button>
<a href="/admins/index">账户列表</a>
</form>
</body>
</html>