数据编辑与PHP实现带省略标志的算法分页
效果展示:
一、数据编辑与删除
1、定义数据库(databases.php)
<?php
namespace zhangfugen;
return [
'type' => $type ?? 'mysql',
'host' => $host ?? 'localhost',
'dbname' => $dbname ?? 'apple',
'port' => $port ?? '3306',
'charset' => $charset ?? 'utf8',
'username' => $username ?? 'root',
'password' => $password ?? '123456'
];
2、连接数据库(connect.php)
<?php
namespace zhangfugen2;
$config = require __DIR__ . '/database.php';
use PDO;
//PDO数据连接~三要素 DSN数据源 username password,
extract($config);
$dsn = sprintf('%s:host=%s;dbname=%s',$type,$host,$dbname);
try {
$pdo = new PDO($dsn,$username ,$password,[PDO::ATTR_ERRMODE=> PDO::ERRMODE_WARNING]);
// var_dump($pdo);
//设置结果集的默认获取方式,只获取关联数组部分
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
} catch(\Exception $e) {
die('Connection error: '. $e->getMessage());
}
3、获取分页数据(userdata.php)
//获取分页数据
// 定义每页显示的数量
$num = 10;
//当前页码,默认为1
$page = $_GET['p'] ?? 1;
//计算偏移量(偏移量 = (页码 -1) \* 每页的显示数量)
$offset = ($page - 1) * $num ;
//获取分页数据
$sql = " SELECT `id`,`uname`,`age`,`type`,`skill` FROM `users` ORDER BY `id` ASC LIMIT {$num} OFFSET {$offset} ";
$users = $pdo->query($sql)->fetchAll();
//获取总页数,ceil() 函数向上舍入为最接近的整数
$sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `users`";
$pages = $pdo->query($sql)->fetch()['total'];
4、控制分发器:编辑+删除(userhandle.php)
<?php
require 'connect.php';
//获取get参数
$action = $_GET['action'];
$id = $_GET['id'];
switch(strtolower($action)):
//编辑操作 :1.渲染编辑表单
// 1.渲染编辑表单
case 'edit':
require 'useredit.php';
break;
//2.执行编辑操作
case 'doedit':
//更新数据表数据
$sql = 'UPDATE `users` SET `uname`= ?, `age` = ?, `type` = ?, `skill` = ? WHERE `id` =?;';
$stmt = $pdo->prepare($sql);
$stmt->execute([$_POST['uname'],$_POST['age'],$_POST['type'],$_POST['skill'],$id]);
if($stmt->rowCount() == 1) echo '<script>alert("更新成功");location.href = "userlist2.php"</script>';
break;
// 删除
case 'delete':
$sql = 'DELETE FROM `users` WHERE `id`=?';
$stmt = $pdo->prepare($sql);
$stmt->execute([$id]);
if ($stmt->rowCount() == 1) echo '<script>alert("删除成功");location.href="userlist2.php"</script>';
default:
die('非法操作');
break;
endswitch;
5、用户编辑页(useredit.php)
效果展示:
源码分享:
<?php
//获取要被编辑的某条数据
$user = $pdo->query(" SELECT * FROM `users` WHERE `id` = {$id}")->fetch();
// print_r($user);
?>
<!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="editstyle.css">
</head>
<body>
<h3>用户编辑</h3>
<form action="<? echo $_SERVER['PHP_SELF'].'?action=doedit&id='.$id ?>" method="POST">
<p>
<label for="uname">名 称</label>
<input type="text" id="uname" name="uname" value="<?=$user['uname']?>">
</p>
<p>
<label for="age">年 龄</label>
<input type="number" name="age" id="age" value="<?=$user['age']?>">
</p>
<p>
<label for="type">工 种</label>
<input type="text" name="type" id="type" value="<?=$user['type']?>">
</p>
<p>
<label for="skill">擅长技能</label>
<input type="text" name="skill" id="skill" value="<?=$user['skill']?>">
</p>
<p>
<button>保存</button>
</p>
</form>
</body>
</html>
编辑页CSS(editstyle.css)
* {
margin: 0;
padding: 0;
box-sizing: border-box;
color: #555;
}
body {
display: flex;
flex-direction: column;
align-items: center;
background-color: #e8f4f8;
}
/*表单样式*/
form {
width: 400px;
border-collapse: collapse;
text-align: center;
background-color: lightblue;
padding: 20px;
}
h3 {
font-size: 1.3rem;
margin: 10px;
}
form p {
padding: 7px;
font-size: 0.8rem;
}
label,
input {
padding: 2px;
font-size: 0.9rem;
line-height: 25px;
}
button {
width: 250px;
padding: 7px;
}
二、数据列表页面(userlist2.php)
算法分页实现原理:1.首页,2.尾页,3.上一页,4.下一页,5.省略 等功能
<?php
require 'userdata.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="userstyle.css">
</head>
<body>
<table>
<caption>【久益一修】装修工人信息表</caption>
<thead>
<tr>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>工种</td>
<td>擅长技能</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<?php foreach($users as $user):?>
<tr>
<td><?= $user['id']?></td>
<td><?= $user['uname']?></td>
<td><?= $user['age']?></td>
<td><?= $user['type']?></td>
<td><?= $user['skill']?></td>
<td>
<button onclick="location.href='userhandle.php?action=edit&id=<?=$user['id']?>'">编辑</button>
<button onclick="location.href='userhandle.php?action=delete&id=<?=$user['id']?>'">删除</button>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<!-- ======================================= -->
<?php
// 分页条显示的页数
$showPages =5;
// 分页条的起始页码值
$startPage = 1;
// 分页条的终止页码值
$endPage = $pages;
// 分页条的终止页码相对于当前页码的偏移量: 分页条显示的页数减1再除2
$offsetPage = ($showPages - 1) / 2;
// 只有当前分页条显示页码数量 < 总页数时, 才有必要显示出省略标志
if ($showPages < $pages) {
//如果当前页码大于偏移量加1, 应该显示省略标记
if ($page > $offsetPage + 1) {
$startOmit = '...';
}
// 如果当前页, 大于偏移量, 就需要重置一下新分页条的起止点页码
if ($page > $offsetPage) {
// 当前分页条的起始页码 = 当前页码 - 偏移量
$startPage = $page - $offsetPage;
// 当前分页条的结束页码 = 当前页码 + 偏移量
$endPage = $page + $offsetPage;
// 当前页码 + 偏移量, 有可能会大于总页数,所以要进行检测
if ($endPage > $pages) {$endPage = $pages; }
} else {
// 如果当前页码 < 偏移量, 如当前是第2页,当前页码就等于偏移量
// 此时需要重新设置新分页条的起始页码
$startPage = 1;
//结束页码就等于显示页码数量
$endPage = $showPages;
}
// 如果当前页 + 偏移量 > 总页数
if ($page + $offsetPage > $pages) {
// 例如总页码10, 当前为9页, 则9+2>10, 此时需要向前面借位偏移,才能保证仍然显示为5个页码
// 此时, 新的起始位置 = 当前位置 - (当前页 + 偏移量 - 原来的结束位置)
$startPage = $page - $offsetPage-1 ;
}
// 只要当前显示的页码数量 < 总页数 并且 当前页+偏移量也小于总页数, 那就应该显示出后面的省略号
if ($showPages < $pages && $page + $offsetPage < $pages) $endOmit = '...';
}
?>
<!-- ======================================= -->
<p>
<!-- 首页-->
<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="javascript:;"><?php echo $startOmit ?></a><?php endif ?>
<!-- ======================================= -->
<!-- 显示页码-->
<!--显示页码是变量:$startPage, 动态的与新分页条保持一致-->
<?php for ($i = $startPage; $i <= $endPage; $i++) : ?>
<!-- 当前页面高亮显示 -->
<?php $active = ($i == $page) ? 'active' : null;
// 跳转页码的地址
$jump = sprintf('%s?p=%s', $_SERVER['PHP_SELF'], $i);
?>
<a href="<?php echo $jump ?>" class="<?php echo $active ?>"><?php echo $i ?></a>
<?php endfor; ?>
<!-- ======================================= -->
<!-- 下一页-->
<!-- 下一页=当前页+1,如果当前页=总页数,下一页=总页数 ,防止向后越界-->
<?php $next = $page + 1; if ($page == $pages) { $next = $pages; } ?>
<!--省略标记插入:在下一页的前面-->
<?php if (isset($endOmit)): ?><a href="javascript:;"><?php echo $endOmit ?></a><?php endif ?>
<a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' . $next ?>">下一页</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' . $pages ?>">尾页</a>
</p>
</body>
</html>
列表页CSS(userstyle.css)
* {
margin: 0;
padding: 0;
box-sizing: border-box;
color: #555;
}
body {
display: flex;
flex-direction: column;
align-items: center;
}
/*表格样式*/
table {
width: 90%;
border: 1px solid;
border-collapse: collapse;
text-align: center;
}
table caption {
font-size: 1.3rem;
margin: 10px;
}
table td,
table th {
border: 1px solid;
padding: 7px;
font-size: 0.9rem;
}
table tr:hover {
background-color: #eee;
}
table thead tr:only-of-type {
background-color: lightblue;
}
table button {
width: 56px;
height: 26px;
}
table button:last-of-type {
color: coral;
}
table button {
cursor: pointer;
margin: 0 3px;
}
/*分页条样式*/
body > p {
display: flex;
}
p > a {
text-decoration: none;
color: #555;
border: 1px solid;
padding: 5px 10px;
margin: 10px 2px;
}
.active {
background-color: coral;
color: white;
border: 1px solid coral;
}