Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
/**
* $total :总信息条数
*
*/
$db = new PDO('mysql:host=localhost;dbname=test','root','901026yk');
$sql = <<<SQL
select count(*) as total from student;
SQL;
$stmt1 = $db->prepare($sql);
$stmt1->execute();
$stmt1->bindColumn(1,$total);
$stmt1->fetch(PDO::FETCH_ASSOC);
//获取总条数
$page = $_GET['p'] ?? 1;
$num = 2; //每页显示的数据条数
$pages = ceil($total/$num);
$offset = ($page - 1) * $num;
$sql = <<<SQL
select * from student limit $offset,$num;
SQL;
$stmt = $db->prepare($sql);
$stmt->execute();
$staff = $stmt->fetchAll(PDO::FETCH_ASSOC);
<?php include './demo01.php' ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
table {
width: 400px;
border-collapse: collapse;
text-align: center;
}
table th,
table td {
border: 1px solid;
padding: 5px;
}
table thead {
background-color: lightcyan;
}
table caption {
font-size: larger;
margin-bottom: 8px;
}
body>p {
display: flex;
}
p>a {
text-decoration: none;
color: #555;
border: 1px solid;
padding: 5px 10px;
margin: 10px 2px;
}
.active {
background-color: seagreen;
color: white;
border: 1px solid seagreen;
}
</style>
</head>
<body>
<table>
<caption>员工信息表</caption>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>学科</th>
</tr>
</thead>
<tbody>
<?php foreach ($staff as $arr):extract($arr) ?>
<tr>
<td><?=$Sno?></td>
<td><?=$Sname?></td>
<td><?=$Sex?></td>
<td><?=$Sage?></td>
<td><?=$Sdept?></td>
</tr>
<?php endforeach ?>
</tbody>
<?php for($i=1;$i<=$pages;$i++):?>
<?php $url = $_SERVER['PHP_SELF'] . '?p=' . $i;
$page = $_GET['p'] ?? 1;
$active = ($i == $page) ? 'active' : null;
?>
<p>
<a href="<?=$url?>" class="<?=$active?>"><?=$i?></a>
</p>
<?php endfor?>
</body>
</html>