代码
model.php
<?php
class Db
{
private $pdo;
public function __construct()
{
$this->pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');
}
//查询显示数据
public function table_select($limit, $page, &$count)
{
$sql = "SELECT COUNT(*) AS 'count' FROM `staffs`";
$sql_obj = $this->pdo->prepare($sql);
$sql_obj->execute();
$count = ceil($sql_obj->fetch()['count'] / 10);
$offset = $limit * ($page - 1);
$sql = "SELECT * FROM `staffs` LIMIT $limit OFFSET $offset";
$sql_obj = $this->pdo->prepare($sql);
$sql_obj->execute();
return $sql_obj->fetchall();
}
//更新数据
public function update($array)
{
extract($array);
$time = strtotime($time);
$sql = "UPDATE `staffs` SET `name`='$name',`sex`='$sex',`position`='$position',`tel`='$tel',`time`='$time' WHERE `id`='$id'";
$sql_obj = $this->pdo->prepare($sql);
$sql_obj->execute();
if ($sql_obj->rowCount() > 0) :
return true;
else :
return false;
endif;
}
//删除数据
public function del($id)
{
$sql="DELETE FROM `staffs` WHERE `id`=$id";
$sql_obj = $this->pdo->prepare($sql);
$sql_obj->execute();
if ($sql_obj->rowCount() > 0) :
return true;
else :
return false;
endif;
}
}
view.php
<?php
class View
{
private $html = <<< head
<link rel="stylesheet" href="style.css">
<table>
<caption>会员管理系统</caption>
<tr>
<th>ID</th><th>姓名</th><th>性别</th><th>职业</th><th>电话</th><th>注册时间</th><th>操作</th>
</tr>
head;
//显示主页
public function index($array, $count, $page)
{
$path = $_SERVER['PHP_SELF'];
$tr = <<< tr
<tr>
<th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th>
<tr>
tr;
$deit = <<<button
<button onclick="location.href='%s'">编辑</button>
<button onclick="location.href='%s'" style="color:red">删除</button>
button;
foreach ($array as $value) {
$time = date('Y/m/d', $value[5]);
$request_edit = $path . "?action=edit&ID=$value[0]&name=$value[1]&sex=$value[2]&position=$value[3]&tel=$value[4]&time=$time";
$request_del = $path . '?action=del&id=' . $value[0];
$html .= sprintf(
$tr,
$value[0],
$value[1],
$value[2],
$value[3],
$value[4],
$time,
sprintf($deit, $request_edit, $request_del)
);
}
return $this->html . $html . '</table>' . $this->show_page($count, $page);
}
//显示分页页码
private function show_page($count, $page)
{
$path = $_SERVER['PHP_SELF'];
$a = "<a href='$path?p=%s' class='%s'>%s</a>";
$start = $page - 3;
if ($start < 1) : $start = 1;
else : $frist = true;
endif;
if ($count - $page < 3) $start = $count - 6;
if ($page <= 1) $page = 1;
$html .= sprintf($a, $page - 1 ? $page - 1 : 1, '', '上一页');
if ($frist && $page > 5) :
$html .= sprintf($a, 1, '', '1');
$html .= sprintf($a, 1, '', '2');
endif;
for ($i = $start; $i < $start + 7; $i++) {
$class = $i == $page ? 'active' : '';
$title = $i;
if ($i == ($start + 6) && $count - $page > 3) $title = '......';
if ($frist) : $title = '......';
$frist = false;
endif;
$html .= sprintf($a, $i, $class, $title);
}
if ($count - $page > 4) :
$html .= sprintf($a, $count - 1, '', $count - 1);
$html .= sprintf($a, $count, '', $count);
endif;
$html .= sprintf($a, ($page + 1) < $count ? $page + 1 : $count, '', '下一页');
return '<p>' . $html . '</p>';
}
public function edit($array)
{
extract($array);
$request = $_SERVER['PHP_SELF'] . '?action=update';
$html = require('edit.php');
return sprintf($html, $request, $ID, $name, $sex, $position, $tel, $time);
}
}
index.php
<?php
require('view.php');
require('model.php');
//服务容器类
class container
{
private $obj = [];
public function bind($name, $func)
{
$this->obj[$name] = $func;
}
public function make($name, $params = [])
{
return call_user_func_array($this->obj[$name], $params);
}
public function destroy()
{
unset($this->obj);
}
}
//绑定
$con = new container();
$con->bind('Db', function () {
return new Db();
});
$con->bind('View', function () {
return new View();
});
$db = $con->make('Db');
$index = $con->make('View');
//判断显示内容
switch (filter_input(INPUT_GET, 'action')) {
case null:
$p = $_GET['p'];
if ($p === null) $p = 1;
echo $index->index($db->table_select(10, $p, $count), $count, $p);
break;
case 'edit':
echo $index->edit($_GET);
break;
case 'del':
if ($db->del($_GET['id'])) :
echo '删除成功<br><a href="index.php">返回</a>';
else :
echo '删除失败<br><a href="index.php">返回</a>';
endif;
break;
case 'update':
if ($db->update($_POST)) :
echo '修改成功<br><a href="index.php">返回</a>';
else :
echo '修改失败<br><a href="index.php">返回</a>';
endif;
break;
default:
break;
}
$con->destroy();
edit.php
<?php
return <<< html
<link rel="stylesheet" href="style.css">
<div>
<h3 class='in'>修改用户信息</h3>
<form action="%s" method="post">
<div class='in'>
<label for="id">ID</label>
<input type="text" name="id" id="id" value="%s"></div>
<div class='in'><label for="name">姓名</label>
<input type="text" name="name" id="name" value="%s"></div>
<div class='in'><label for="sex">性别</label>
<input type="text" name="sex" id="sex" value="%s"></div>
<div class='in'><label for="position">职业</label>
<input type="text" name="position" id="position" value="%s"></div>
<div class='in'><label for="tel">电话</label>
<input type="text" name="tel" id="tel" value="%s"></div>
<div class='in'><label for="time">注册时间</label>
<input type="text" name="time" id="time" value="%s"></div>
<input type="submit" id='submit' value="修改">
</form>
</div>
html;
style.css
td,
th {
border: 1px solid black;
}
table {
width : 80%;
border-collapse: collapse;
}
caption {
font-size: 1.9rem;
margin : 10px;
}
body {
display : flex;
flex-direction: column;
align-items : center;
}
p {
margin-top: 20px;
}
p>a {
text-decoration: none;
color : #555;
border : 1px solid;
padding : 5px 10px;
margin : 10px 2px;
}
.active {
background-color: red;
color : white;
border : 1px solid red;
}
label {
width : 80px;
display: inline-block;
}
h3 {
text-align: center;
}
#submit {
float : right;
width : 100px;
height : 30px;
margin-top: 20px;
}