<script src="{{asset('js/jquery-3.4.1.min.js')}}"></script>
<center>
<h2>展示用户</h2>
<input type="text" class="sou"><button onclick="fun()">搜索</button>
<br>
<a href="/login/add">添加用户</a> | <a href="/login">退出登录</a> | <a href="/role">角色展示</a> | <a href="/login/show">用户展示</a>
<table border="1">
<tr>
<td><button class="quan">全选</button> <button class="noquan">全不选</button></td>
<td>id</td>
<td>用户名</td>
<td>密码</td>
<td>操作</td>
</tr>
<tbody class="tbody1">
</tbody>
</table>
<div class="page">
</div>
</center>
<script>
fun(1);
function fun(page){
var sou = $(".sou").val();
$.ajax({
url:"/loginPage",
type:"post",
dataType:"json",
data:{
page:page,
sou:sou
},
success:function(res){
$(".tbody1").html(res['arr']);
$(".page").html(res['but'])
}
})
}
$(".quan").click(function(){
var box = document.getElementsByName('box');
for (i = 0; i<box.length;i++){
if (box[i].checked == false) {
box[i].checked = true;
}
}
})
$(".noquan").click(function(){
var box = document.getElementsByName('box');
for (i=0;i<box.length;i++){
if (box[i].checked == true){
box[i].checked = false;
}
}
})
</script>
//php 代码
public function loginPage()
{
$page = isset($_POST['page'])?$_POST['page'] : 1;//当前页
$sou = isset($_POST['sou'])?$_POST['sou'] : '';
// echo $page;die;
$size = 3;//每页3条
$res = DB::select("select * from user where username like '%$sou%'");
$count = count($res);//一共条数
// echo $count;die;
$bigpage = ceil($count/$size);//最大页数
$foll = ($page-1)*$size;//偏移量
$prev = $page - 1 < 1 ? 1 : $page -1;//上一页
$next = $page + 1 > $bigpage ? $bigpage : $page + 1;//下一页
$data = DB::select("select * from user where username like '%$sou%' limit $foll,$size");
$data = json_decode(json_encode($data),1);
// echo json_encode($data);die;
$arr = "";
foreach($data as $key => $v){
$arr .= "
<tr>
<td><input type=\"checkbox\" name=\"box\" value=".$v['id']."></td>
<td>".$v['id']."</td>
<td>".$v['username']."</td>
<td>".$v['userpassword']."</td>
<td></td>
</tr>
";
}
$but = "
<button onclick='fun(1)'>首页</button>
<button onclick='fun($prev)'>上一页</button>
<button onclick='fun($next)'>下一页</button>
<button onclick='fun($bigpage)'>尾页</button>
";
$ser['arr'] = $arr;
$ser['but'] = $but;
echo json_encode($ser);
}