Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:部分实现了功能需求
用MVC的模式演示分页
model.php
<?php
class Dbdata
{
private $pdo;
public function __construct()
{
$this->pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');
}
/**
* 录入数据
*
* @param [type] $array 要录入的数据数组
* @return void
*/
public function insert($array)
{
$sql = "INSERT `staffs` SET
`name`='$array[0]',
`sex`='$array[1]',
`position`='$array[2]',
`tel`='$array[3]',
`time`=$array[4]";
$this->pdo->prepare($sql)->execute();
return var_dump($this->pdo->lastInsertId()) ? true : false;
}
/**
* 返回每一页的查询结果
*
* @param [type] $limit 每页显示的条数
* @param [type] $offset 查询偏移
* @param [type] $count 页数
* @return void
*/
public function select($limit, $offset, &$count)
{
$sql = "SELECT * FROM `staffs` LIMIT $limit OFFSET $offset";
$sql_count = "SELECT COUNT(*) AS `count` FROM `staffs` LIMIT 10 OFFSET 0";
$sql_obj = $this->pdo->prepare($sql_count);
$sql_obj->execute();
$count = ceil(($sql_obj->fetch()['count'] / $limit));
$sql_obj = $this->pdo->prepare($sql);
$sql_obj->execute();
return $sql_obj->fetchAll();
}
}
/*
function name($size)
{
$name = 'abcdefghijklmnopqrstuvwxyz';
for ($i = 0; $i < $size; $i++) {
$result .= $name{
rand(0, 25)};
}
return $result;
}
$b = new Dbdata();
$name = name(8);
$sex = ['男', '女'][rand(0, 1)];
$zy = ['工人', '学生', '医生', '律师', '军人', '销售', '文员'][rand(0, 6)];
$tel = rand(13000000000, 13999999999);
$time = rand(1500000000, 1599999999);
$a = [$name, $sex, $zy, $tel, $time];
//$b->insert($a);
print_r($b->select('10','10',$count));
echo '运行完毕';
*/
view.php
<?php
class view
{
/**
* 显示主页表格
*
* @param [type] $array 要输入到表格中的数据
* @param [type] $count 数据的总页数
* @param [type] $page 当前所在的页数
* @return void
*/
public function index($array, $count = null, $page = null)
{
$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>
</tr>
head;
foreach ($array as $key => $value) {
$date = date('Y/m/d', $value[5]);
$html .= <<< tab
<tr>
<th>$value[0]</th>
<th>$value[1]</th>
<th>$value[2]</th>
<th>$value[3]</th>
<th>$value[4]</th>
<th>$date</th>
</tr>
tab;
}
$html .= '</table>';
$html .= $this->pageshow($count, $page);
echo $html;
}
/**
* 返回翻页标签的mthl代码
*
* @param [type] $count 总页数
* @param [type] $page 当前页数
* @return void
*/
private function pageshow($count, $page)
{
$path = $_SERVER['PHP_SELF'];
$html .= '<p>';
$p_a = $page - 1;
if ($page > 1) $html .= "<a href='$path?p=$p_a'>上一页</a>";
$p_c = $page - 4;
if ($page - 5 > 0) :
$p_d = $p_c - 1;
$html .= <<<a
<a href="$path?p=$p_d" >...</a>
a;
if ($count - $page <= 4) :
$ii = $count - 9;
else :
$ii = $p_c;
endif;
else :
$ii = 1;
endif;
for ($i = $ii; $i < $count; $i++) {
$a += 1;
if ($a > 9) :
$html .= <<<a
<a href="$path?p=$i" >...</a>
a;
break;
endif;
$class = ($i == ($p_a + 1)) ? 'active' : '';
$html .= <<<a
<a href="$path?p=$i" class='$class'>$i</a>
a;
}
$p_b = $page + 1;
if ($p_b <= $count) $html .= "<a href='$path?p=$p_b'>下一页</a>";
$html .= '</p>';
return $html;
}
}
index.php
<?php
$p=$_GET['p'];
if($p===null||$p<1)$p='1';
require('view.php');
require('model.php');
$view= new view();
$mode=new Dbdata();
$data=$mode->select(10,10*($p-1),$count);
if(!$data)$data=$mode->select(10,10*($count-1),$count);
if($p>$count)$p=$count;
$view->index($data,$count,$p);
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;
}