Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:目前处于学习阶段, 写对, 比什么都重要 ....
<?php
$dsn = 'mysql:host=localhost;dbname=phpedu;charset=utf8;port=3306';
$username = 'root';
$password = 'root';
try {
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (Exception $e) {
die($e->getMessage());
}
<?php
require 'connect.php';// 连接数据库
$page = $_GET['p'] ?? 1;//设置当前的页数/页码
$num = 5;// 设置每页显示的记录数量
$sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `staffs`";
$pages = $pdo->query($sql)->fetch()['total'];// 设置总页数
$offset = $num * ($page - 1);// 设置偏移量
$sql = "SELECT * FROM `staffs` LIMIT {$num} OFFSET {$offset}";
$staffs = $pdo->query($sql)->fetchAll();// 设置分页数据
<?php require 'demo1.php' ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>首页</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<nav>
<?php if (isset($user)) : ?>
<a href="" id="logout"><span style="color:red"><?php echo $user['name']?></span> 退出</a>
<?php else: ?>
<ul>
<li><a href="#" title="首页" target="_blank">首页</a></li>
<li><a href="#" title="音乐" target="_blank">音乐</a></li>
<li><a href="#" title="舞蹈" target="_blank">舞蹈</a></li>
<li><a href="#" title="绘画" target="_blank">绘画</a></li>
<li><a href="#" title="雕塑" target="_blank">雕塑</a></li>
<li><a href="#" title="书法" target="_blank">书法</a></li>
<li><a href="#" title="戏曲" target="_blank">戏曲</a></li>
<li><a href="#" title="相声" target="_blank">相声</a></li>
<li><a href="#" title="小品" target="_blank">小品</a></li>
<li><a href="#" title="戏曲" target="_blank">非遗</a></li>
<li><a href="session/login.php" title="登录" target="_blank">登录</a></li>
<li><a href="session/register.php" title="注册" target="_blank">注册</a></li>
</ul>
</nav>
<?php endif ?>
<table>
<caption>文化馆职工管理系统</caption>
<thead>
<tr>
<th>I D</th>
<th>姓 名</th>
<th>年 龄</th>
<th>性 别</th>
<th>职 位</th>
<th>手 机</th>
<th>入职时间</th>
<th>操 作</th>
</tr>
</thead>
<tbody>
<?php foreach ($staffs as $staff) : ?>
<tr>
<td><?php echo $staff['id'] ?></td>
<td><?php echo $staff['name'] ?></td>
<td><?php echo $staff['age'] ?></td>
<td><?php echo $staff['sex'] ? '男' : '女' ?></td>
<td><?php echo $staff['position'] ?></td>
<td><?php echo $staff['mobile'] ?></td>
<td><?php echo date('Y-m-d', $staff['hiredate']) ?></td>
<td><button onclick="location.href='handle.php?action=edit&id=<?php echo $staff['id'] ?>'">编辑</button>
<button onclick="del(<?php echo $staff['id'] ?>)">删除</button></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<!-- 添加跳转到首页, 前一页, 下一页, 尾页的功能 -->
<p>
<?php
$showPages = 5;// 分页条显示5个页码
$startPage = 1;// 分页条的起始页码
$endPage = $pages; // 分页条的终止页码
$offsetPage = ($showPages -1) / 2; // (当前分页条显示的页码数 - 1) / 2
// 设置显示省略标记
if ($showPages < $pages) {
if ($page > $offsetPage + 1) {
$startOmit = '...';
}
if ($page > $offsetPage) {
$startPage = $page - $offsetPage;
$endPage = $page + $offsetPage;
if ($endPage > $pages) {$endPage = $pages;}
} else {
$startPage = 1;
$endPage = $showPages;
}
if ($page + $offsetPage > $pages) {
$startPage = $startPage - ($page + $offsetPage - $endPage);
}
if ($showPages < $pages && $page + $offsetPage < $pages) $endOmit = '...';
}
?>
<!-- 首页 -->
<a href="<?php echo $_SERVER['PHP_SELF'] . '?p=1' ?>">首页</a>
<!-- 前一页 -->
<?php
$prev = $page - 1;
if ($page == 1) $prev = 1;
?>
<a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' . $prev ?>">前一页</a>
<?php if (isset($startOmit)) : ?> <a href="#"><?php echo $startOmit ?></a> <?php endif ?>
<?php for ($i=$startPage; $i<=$endPage; $i++): ?>
<?php
$jump = sprintf('%s?p=%s', $_SERVER['PHP_SELF'], $i );
$active = ($i == $page) ? 'active' :null;
?>
<a href="<?php echo $jump ?>" class="<?php echo $active ?>"><?php echo $i ?></a>
<?php endfor ?>
<?php if (isset($endOmit)) : ?> <a href="#"><?php echo $endOmit ?></a> <?php endif ?>
<!-- 下一页 -->
<?php
$next = $page + 1;
if ($page == $pages) $next = $pages;
?>
<a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' . $next ?>">下一页</a>
<!-- 尾页 -->
<a href="<?php echo $_SERVER['PHP_SELF'] . '?p='. $pages ?>">尾页</a>
</p>
<script>
function del(id) {
return confirm('是否删除?') ? alert('删除成功') : false;
}
</script>
</body>
</html>
<?php
// 获取被编辑的员工信息
$staff = $pdo->query("SELECT * FROM `staffs` WHERE `id`={$id}")->fetch();
// print_r($staff);//测试打印数据
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h3>编辑员工信息</h3>
<form action="<?php echo $_SERVER['PHP_SELF'].'?action=doedit&id='. $id ?>" method="post">
<p>
<label for="name">姓名</label>
<input type="text" id="name" name="name" value="<?php echo $staff['name'] ?>">
</p>
<p>
<label for="age">年龄:</label>
<input type="number" id="age" name="age" value="<?php echo $staff['age'] ?>">
</p>
<input type="hidden" name="id" value="<?php echo $staff['id'] ?>">
<p>
<label for="sex">性别:</label>
<input type="radio" id="sex" name="sex" value="1" <?php if ($staff['sex'] == 1) echo 'checked' ?>><label for="">男</label>
<input type="radio" id="sex" name="sex" value="0" <?php if ($staff['sex'] == 0) echo 'checked' ?>><label for="">女</label>
</p>
<p>
<label for="position">职位:</label>
<input type="text" id="position" name="position" value="<?php echo $staff['position'] ?>">
</p>
<p>
<label for="mobile">手机号:</label>
<input type="text" id="tel" name="mobile" value="<?php echo $staff['mobile'] ?>">
</p>
<p>
<label for="hiredate">手机号:</label>
<input type="text" id="hiredate" name="hiredate" value="<?php echo date('Y-m-d', $staff['hiredate']) ?>">
</p>
<p>
<button>保存</button>
</p>
</form>
</body>
</html>
<?php
require 'demo1.php';
$action = $_GET['action'];
$id = $_GET['id'];
switch ($action) {
// 编辑需要进行两步
// 1. 渲染编辑表单
case 'edit':
include 'edit.php';
break;
// 2. 执行编辑操作
case 'doedit':
$sql = 'UPDATE `staffs` SET `name`=:name, `age`=:age,`sex`=:sex,`position`=:position ,`mobile`=:mobile,`hiredate`=:hiredate WHERE `id`=:id';
// print_r($_POST);
// $_POST['id'] = $id;
$_POST['hiredate'] = strtotime($_POST['hiredate']);
$stmt = $pdo->prepare($sql);
$stmt->execute($_POST);
if ($stmt->rowCount() === 1) echo '<script>alert("更新成功");location.href="demo4.php";</script>';
// 3. 删除员工数据
case 'del':
$stmt = $pdo->query('delete from `staffs` where `id`=$id');
if ($stmt->rowCount() === 1) echo '<script>alert("删除成功");location.href="demo4.php";</script>';
break;
}
对于在服务器上处理表格数据有了大体的概念,虽然目前学到的知识是零散的,当然还有些迷惑,但是脑子里已经有了基本的呈现思路,对于呈现这些思路的工具和命令必然要十分熟悉,而且我认为获得最佳的用户体验(俗称用得顺手)是程序员不懈追求的动力。