批改状态:合格
老师批语:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
table,td,th{
border:1px solid #000;
}
table{
border-collapse: collapse;
margin: 10px auto;
width: 70%;
}
th{
background: lightblue
}
td{
text-align:center;
}
h4{
text-align: center;
}
h4 a{
display: inline-block;
margin: 10px auto;
text-align: center;
margin:10px;
padding:5px 10px;
border: 1px solid #888;
text-decoration: none;
color: #000;
background: lightblue;
}
.active{
background-color:burlywood;
}
</style>
<body>
<?php
$conn=mysqli_connect('127.0.0.1','root','root','mytest');//创建连接
$page=isset($_GET['p'])? $_GET['p']:1;//GET方式获取页码
$num=5;//设定每页显示个数
$offset=($page-1)*$num;//设置偏移量
$sql="SELECT * FROM user limit {$offset},{$num};";//创建sql语句
$res=mysqli_query($conn,$sql);//提交sql
$rows=mysqli_fetch_all($res,MYSQLI_ASSOC);//查询所有数据行
//获取总页数
$number = mysqli_query($conn,"SELECT COUNT(*) FROM user");//提交查询,查询总数
list($total)=mysqli_fetch_row($number);//将查询总数保存在$total中
$pages=ceil($total/$num);//向上取整函数
//解决端值溢出问题
$page = ($page==1)?1:$page;
$page = ($page==$pages+1)?$pages:$page;
?>
<table>
<caption><h2>员工信息表</h2></caption>
<tr>
<th>id</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>工资</th>
</tr>
<?php foreach($rows as $row):?>
<tr>
<td><?php echo $row['ID']?></td>
<td><?php echo $row['name']?></td>
<td><?php echo $row['sex']?></td>
<td><?php echo $row['age']?></td>
<td><?php echo $row['salary']?></td>
</tr>
<?php endforeach;?>
</table>
<h4>
<a href="http://127.0.0.1/homework/0428/index.php?p=1">首页</a>
<?php if ($page!=1):?>
<a href="http://127.0.0.1/homework/0428/index.php?p=<?php echo $page-1;?>">上页</a>
<?php endif;?>
<?php for ($i=0;$i<$pages;$i++):?>
<a class="<?php if ($_GET['p']==$i+1){echo 'active';}?>" href="http://127.0.0.1/homework/0428/index.php?p=<?php echo $i+1?>"><?php echo $i+1?></a>
<?php endfor;?>
<?php if ($page!=$pages):?>
<a href="http://127.0.0.1/homework/0428/index.php?p=<?php echo $page+1;?>">下页</a>
<?php endif;?>
<a href="http://127.0.0.1/homework/0428/index.php?p=<?php echo $pages;?>">尾页</a>
</h4>
</body>
</html>点击 "运行实例" 按钮查看在线实例
效果演示

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号