PHP分页功能扩展及数据的修改删除
- connect.php 连接数据库
- page-list.php显示分页效果
- page-data.php分页处理
- handle.php 处理修改和删除
- edit.php 修改的信息提交
- style.css 分页显示样式
1.connect.php 连接数据库
<?php
// 连接数据库
// PDO连接对象
$pdo = new PDO('mysql:host=localhost;dbname=mysqli', 'root', '142536');
// 设置结果默认获取方式: 关联数组
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
2.page-list.php
<?php
require 'page-data.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>
<td>id</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>邮件</td>
<td>手机</td>
<td>操11作</td>
</tr>
</thead>
<tbody>
<?php foreach ($show as $users):?>
<tr>
<!-- 短标签来简化变量的显示 -->
<td><?=$users['id']?></td>
<td><?=$users['username'] ?></td>
<td><?=$users['sex'] ?></td>
<td><?=$users['age'] ?></td>
<td><?=$users['email']?></td>
<td><?=$users['mobile'] ?></td>
<td><button onclick="location.href='handle.php?action=edit&id=<?=$users['id']?>'" >编辑</button>
<button onclick="location.href='handle.php?action=del&id=<?=$users['id']?>'" >删除</button></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<p>
<?php show($pages,$page)?>
</p>
</body>
</html>
3.page-data.php分页处理
<?php
// 连接数据库
require 'connect.php';
//获取分页数据,一定要知道的二个数据
//1.每页显示的数量
$zpage = $_GET['zpage'];
if($zpage == "")
{
$num = 5;
}
else
{
$num = $zpage;
}
//2.当前页码,默认为1
$page = $_GET['p'] ?? 1;
//3.计算每一页的第一条记录的显示偏移量
$offset =($page - 1) * $num;
//4.获取分页数据
$sql = "SELECT * FROM `user` LIMIT {$num} OFFSET {$offset}";
$show = $pdo->query($sql)->fetchAll();
//计算总页数 SELECT COUNT(`ID`) AS `total` FROM `user`
//计算表中共有多少条记录
//每一页显示几条
//总页数 = ceil(记录总数/每页的记录数) 向上取整 相当于9.1等于10
$sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `user`";
$pages=$pdo->query($sql)->fetch()['total'];
function show($pages,$page)
{
$w = $pages-1;
$c = $pages-10;
$w1 =$pages-13;
$w2 =$page-7;
$w3 =$page+7;
$show = "<a href=".$_SERVER['PHP_SELF'].">首页</a>";
$prev =$page-1;
if($prev == 0) $prev = 1;
$show .= "<a href='{$_SERVER['PHP_SELF']}?p={$prev}'>上一页</a>";
if($pages<10)
{
for($i = 1 ;$i<=$pages;$i++)
{
$href=sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);
$active = ($i == $page) ? 'active':NULL;
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$i}' class='{$active}'>{$i}</a>";
}
}
if($pages>=10 && $prev<10)
{
for($i = 1 ;$i<=10;$i++)
{
$href=sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);
$active = ($i == $page) ? 'active':NULL;
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$i}' class='{$active}'>{$i}</a>";
}
$show.= "<a href='{$_SERVER['PHP_SELF']}?p=14'>...</a>";
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$w}'>{$w}</a>";
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$pages}'>{$pages}</a>";
}
if($prev >= 10 && $prev < $c)
{
$show.= "<a href='{$_SERVER['PHP_SELF']}?p=1'>1</a>";
$show.= "<a href='{$_SERVER['PHP_SELF']}?p=2'>2</a>";
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$w2}'>...</a>";
for($i = $prev-2 ;$i<=$prev+4;$i++)
{
$href=sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);
$active = ($i == $page) ? 'active':NULL;
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$i}' class='{$active}'>{$i}</a>";
}
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$w3}'>...</a>";
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$w}'>{$w}</a>";
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$pages}'>{$pages}</a>";
}
if($prev >=$c)
{
$show.= "<a href='{$_SERVER['PHP_SELF']}?p=1'>1</a>";
$show.= "<a href='{$_SERVER['PHP_SELF']}?p=2'>2</a>";
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$w1}'>...</a>";
for($i = $pages-9 ;$i<=$pages;$i++)
{
$href=sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);
$active = ($i == $page) ? 'active':NULL;
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$i}' class='{$active}'>{$i}</a>";
}
}
$next = $page + 1;
if($next >=$pages) $next = $pages;
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$next}'>下一页</a>";
$show.= "<a href='{$_SERVER['PHP_SELF']}?p={$pages}'>尾页</a>";
echo $show;
}
4.handle.php 处理修改和删除
<?php
require 'connect.php';
//获取操作
$action = $_GET['action'];
$id = $_GET['id'];
switch($action)
{
case 'edit':
include 'edit.php';
break;
case 'doedit':
$sql = 'UPDATE `user` SET `username`=?,`password`=? WHERE `id`=?;';
$stmt = $pdo->prepare($sql);
if (!empty($_POST)) {
$stmt->execute([$_POST['username'],sha1($_POST['password']), $id]);
if ($stmt->rowCount() == 1) echo '<script>alert("更新成功");location.href="page-list.php"</script>';
}
break;
case 'del':
$sql ='DELETE FROM `user` WHERE `id`=?';
$stmt = $pdo->prepare($sql);
$stmt->execute([$id]);
if ($stmt->rowCount() == 1) echo '<script>alert("删除成功");location.href="page-list.php"</script>';
}
5.edit.php 修改的信息提交
<?php
// 获取要被编辑的数据
$user = $pdo->query("SELECT * FROM `user` 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>
</head>
<body>
<h3>用户编辑</h3>
<form action="<? echo $_SERVER['PHP_SELF']. '?action=doedit&id='.$id?>" method="POST">
<p>
<label for="name">用户名:</label>
<input type="text" name="username" id="username" value="<?=$user['username']?>">
</p>
<p>
<label for="email">密码:</label>
<input type="password" name="password" id="password" value="******">
</p>
<p>
<button>保存</button>
</p>
</form>
</body>
</html>
示例图:
总结:在给分页添加功能,主要是对pages和prev进行处理。