演示代码
演示地址 http://php.rc238.cn/0513/
index.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>
<table>
<caption>
员工管理系统
</caption>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>职位</th>
<th>手机</th>
<th>入职时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php
require 'parameter.php';
foreach ($staffs as $staff) {
// 数组转名值对
extract($staff);
/* @var string $id ;
* @var string $name ;
* @var string $age ;
* @var string $sex ;
* @var string $position ;
* @var string $mobile ;
* @var string $hiredate ;
* */
$sex = $sex ? '男' : '女';
$hiredate = date('Y / m / d', $hiredate);
echo <<<AAA
<tr>
<td>{$id}</td>
<td>{$name}</td>
<td>{$age}</td>
<td>{$sex}</td>
<td>{$position}</td>
<td>{$mobile}</td>
<td>{$hiredate}</td>
<td><button onclick="location.href='handle.php?action=edit&p={$page}&id={$id}'" >编辑</button><button onclick="del('{$id}','{$name}','{$page}')">删除</button></td>
</tr>
AAA;
}
//删除数据
?>
</tbody>
</table>
<div>
<?php
// 分页条数
$showpage = 5;
//起始页码 永远显示前两页
$startpage = 3;
//结束页 永远显示后两页
$endpage = $pages - 2;
//页码偏移量
$offsetpage = ($showpage - 1) / 2;
//开始判断是否有必要显示...
if ($showpage < $pages) {
// 大于6显示前面的省略号
if ($page >= 6){
$startOmit = '...';
}
// 小于总页数-分页条数显示后面省略号
if ($page <= ($pages-$showpage)) {
$endOmit = '...';
}
// 修改起始页码
if ($page >= 6){
$startpage = $page-$offsetpage;
if ($startpage >= $pages-$showpage ){
$startpage = $pages-$showpage;
}
}
// 修改结束页码
if ($page <= $pages-$showpage){
$endpage = $page+$offsetpage;
if ($endpage <= 6){
$endpage = 6;
}
}
}
// 显示首页 和 上一页
if ($page > 1) {
echo "<a href='?p=1' class='headtail'>首页</a>";
$start = $page - 1;
echo "<a href='?p={$start}' class='around'>上一页</a>";
}
// 输出方法
function Output($i)
{
global $page;
$jump = sprintf('?p=%s', $i);
$active = ($i == $page) ? 'active' : null;
echo "<a href='{$jump}' class='{$active}'>{$i}</a>";
}
// 永远显示前两页
for ($i = 1; $i <= 2; $i++) {
Output($i);
}
// 判断显示前面...
if (isset($startOmit)) {
echo '<span>'.$startOmit.'</span>';
}
// 显示中间页码
for ($i = $startpage; $i <= $endpage; $i++) {
Output($i);
}
// 判断显示后面...
if (isset($endOmit)) {
echo '<span>'.$endOmit.'</span>';
}
// 永远显示后两页/
for ($i = $pages - 1; $i <= $pages; $i++) {
Output($i);
}
// 判断显示下一页和尾页
if ($page < $pages) {
$end = $page + 1;
echo "<a href='?p={$end}' class='around'>下一页</a>";
echo "<a href='?p={$pages}' class='headtail'>尾页</a>";
}
// 如果总页数大于10 显示跳转
if ($pages > 10) {
echo <<<bbb
<form class="form1" action="" method="get">
<input type="number" name="p"min="1" max=" {$pages}" required />
<button>跳转</button>
</form>
bbb;
}
?></div>
<script>
function del(id,name,page) {
return confirm('确认删除姓名为"'+name+'"的员工吗?') ? location.href="handle.php?action=del&p="+page+"&id="+id : false;
}
</script>
</body>
</html>
parameter.php
<?php
//连接数据库
require 'connect.php';
//获取当前页码
$page = $_GET['p'] ?? 1 ;
//每页显示数量
$num = 13;
//总页数
$pages = $pdo->query("SELECT CEIL(COUNT(`id`)/{$num}) AS `tatol` FROM `staffs`")->fetch()['tatol'];
//偏移量
$offset = $num * ($page-1);
//分页数据
$staffs = $pdo->query("SELECT * FROM `staffs` LIMIT {$num} OFFSET {$offset}")->fetchAll();
connect.php
<?php
//创建连接参数
$dsn = 'mysql:host=localhost;dbname=php11.edu';
$username = 'php11.edu';
$password = 'php11.edu';
//创建异常类
try {
$pdo = new PDO($dsn,$username,$password);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
}catch (Exception $e){
die($e->getMessage());
}
handle.php
<?php
//先判断有没有传值过来
if (!isset($_GET['action']) || !isset($_GET['p']) || !isset($_GET['id'])) {
die('<script>alert("非法请求");location.href="index.php"</script>');
}
//获取数据
$action = strtolower($_GET['action']);
$p = $_GET['p'];
$id = $_GET['id'];
//连接数据库
require 'connect.php';
//执行
switch ($action) {
case 'edit':
require 'edit.php';
break;
case 'doedit':
// print_r($_POST);
// 执行修改
//执行更新
$sql = "UPDATE `staffs` SET `name` = :name ,`age` = :age , `position` = :position,`mobile` = :mobile WHERE `id` = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute($_POST);
//判断是否执行成功
if ($stmt->rowCount()===1){
echo "<script>alert('修改成功');location.href='index.php?p={$p}'</script>";
}else{
if ($stmt->errorInfo()[0] == 00000){
echo "<script>alert('没有值被修改');location.href='index.php?p={$p}'</script>";
}else{
echo "<script>alert('修改失败');location.href='index.php?p={$p}'</script>";
}
}
break;
case 'del':
$stmt = $pdo->prepare("DELETE FROM `staffs` WHERE `id`={$id}");
$stmt->execute();
if ($stmt->rowCount() === 1) {
echo "<script>alert('删除成功');location.href='index.php?p={$p}'</script>";
} else {
echo "<script>alert('删除失败');location.href='index.php?p={$p}'</script>";
}
break;
}
edit.php
<?php
//更新修改数据库
//先查询到信息显示
$sql = "SELECT * FROM `staffs` WHERE `id`={$id}";
$user = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
extract($user);
/**
* @var string $id ;
* @var string $name ;
* @var string $age ;
* @var string $sex ;
* @var string $position ;
* @var string $mobile ;
* @var string $hiredate ;
**/
$sex = $sex ? '男' : '女';
$date = date('Y / m / d', $hiredate);
echo <<< aa
<style>
@import "style.css";
</style>
<form class="update" action="?action=doedit&id={$id}&p={$p}" method="post">
<div>
<label for="id">ID:</label>
<input type="text" id="id" disabled value="{$id}" />
</div>
<div>
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required value="{$name}" />
</div>
<div>
<label for="age">年龄:</label>
<input type="number" max="99" min="18" id="age" name="age" required value="{$age}" />
</div>
<div>
<label for="sex">性别:</label>
<input type="text" id="sex" disabled value="{$sex}" />
</div>
<div>
<label for="position">职位:</label>
<input type="text" id="position" name="position" required value="{$position}" />
</div>
<div>
<label for="mobile">手机号:</label>
<input
type="text"
id="mobile"
name="mobile"
required
minlength="11"
maxlength="11"
value="{$mobile}"
/>
</div>
<div>
<label for="hiredate">入职时间:</label>
<input type="text" id="hiredate" disabled value="{$date}" />
</div>
<button name="id" value="{$id}">提交</button>
</form>
aa;
style.css
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
color: #555;
}
body {
display: flex;
flex-direction: column;
align-items: center;
}
table {
width: 1000px;
border: 1px solid;
text-align: center;
border-collapse: collapse;
}
table > caption {
font-size: 1.2rem;
margin: 10px;
}
table td,
table th {
border: 1px solid;
padding: 5px;
}
table tr:hover {
background-color: #eee;
}
div {
display: flex;
flex-flow: row nowrap;
align-items: center;
}
div > a {
padding: 5px;
border: 1px solid;
margin: 10px;
width: 30px;
height: 30px;
text-decoration: none;
text-align: center;
}
div > a:hover {
background-color: violet;
color: #fff;
}
.headtail {
width: 50px;
}
.around {
width: 80px;
}
.active {
background-color: lightskyblue;
color: linen;
}
.form1 > input {
width: 50px;
height: 30px;
}
.form1 > input:out-of-range,
.form1 > input::-webkit-inner-spin-button {
appearance: none;
}
.update {
display: flex;
flex-flow: column;
margin: 30px auto;
}
.update > div {
width: 220px;
margin-bottom: 10px;
display: flex;
justify-content: space-between;
}
tbody button {
margin: 0px 10px;
border: none;
border-radius: 5px;
padding: 5px 10px;
background-color: lightskyblue;
color: #fff;
}
tbody button:hover {
background-color: lightgreen;
}
总结
比之前的代码流畅多了