批改状态:合格
老师批语:

<?php /医院 * Created by PhpStorm. * User: Administrator * Date: 2018/4/28 0028 * Time: 下午 3:18 */ $pageSize=5;//每页count $res_array;//这是显示的数据 $rowCount;//这是从数据库中获取的 $pageNow=1;//用户指定的 $offset=0;//偏移量 $pageCount;//这个是计算得到的
点击 "运行实例" 按钮查看在线实例
<?php
/医院
* Created by PhpStorm.
* User: Administrator
* Date: 2018/4/27 0027
* Time: 下午 2:06
*/
header("content-type:text/html;charset=utf-8");
require "everyPageDefine.php";
$dsn = 'mysql:host=127.0.0.1;dbname=work';
$dbname = 'root';
$dbpass = 'root';点击 "运行实例" 按钮查看在线实例
<?php
/医院
* Created by PhpStorm.
* User: Administrator
* Date: 2018/4/27 0027
* Time: 下午 12:56
*/
header("content-type:text/html;charset=utf-8");
/医院
* Class PdoUtil pdo封装
*/
class PdoUtil
{
private $pdo;
private $dsn;
private $dbName;
private $dbPass;
private $option = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //设置错误模式
PDO::ATTR_CASE => PDO::CASE_NATURAL, //数据表字段保持不变
PDO::ATTR_EMULATE_PREPARES => true, //启用PDO模拟
PDO::ATTR_PERSISTENT => true, //启用持久性连接
];
public $stmt;
function PdoUtil($dsn, $dbName, $dbPass)
{
$this->dsn = $dsn;
$this->dbName = $dbName;
$this->dbPass = $dbPass;
try {
$this->pdo = new PDO($this->dsn, $this->dbName, $this->dbPass, $this->option);
} catch (PDOException $e) {
die("连接失败" . $e->getMessage());
}
}
/医院
* Insert
* @param $table
* @param array $data
*/
public function Insert($table, $data = [])
{
$sql = "INSERT IGNORE {$table} SET ";
foreach (array_keys($data) as $filed) {
$sql .= $filed . "=:" . $filed . ", ";
}
//去掉尾部逗号,并添加分号结束
$sql = rtrim(trim($sql), ',') . ';';
$this->stmt = $this->Pre($sql);
//绑定值到预处理对象
foreach ($data as $filed => $value) {
$this->stmt->bindValue(":{$filed}", $value);
}
if ($this->stmt->execute()) {
if ($this->stmt->rowCount() > 0) {
echo "最后插入的id:=" . $this->pdo->lastInsertId();
}
} else {
echo "插入失败";
}
}
/医院
* Update
* @param $table
* @param array $data
* @param $where
* @return bool
*/
public function Update($table, $data = [], $where)
{
$sql = "UPDATE {$table} SET ";
foreach (array_keys($data) as $filed) {
$sql .= $filed . "=:" . $filed . ", ";
}
$sql = rtrim(trim($sql), ',');
if (empty($where)) {
return false;
} else {
$sql .= ' WHERE ' . $where;
}
$this->stmt = $this->Pre($sql);
//绑定值到预处理对象
foreach ($data as $filed => $value) {
$this->stmt->bindValue(":{$filed}", $value);
}
if ($this->stmt->execute()) {
if ($this->stmt->rowCount() > 0) {
echo "更新受影响的函数:=" . $this->stmt->rowCount();
}
} else {
echo "更新失败";
}
}
/医院
* Select
* @param $table
* @param $fields
* @param string $where
* @param string $order
* @return array
*/
public function Select($table, $fields, $where = '', $order = '',$offset,$pageSize)
{
$sql = "SELECT ";
if (is_array($fields)) {
foreach ($fields as $field) {
$sql .= $field . ', ';
}
} else {
$sql .= $fields;
}
$sql = rtrim(trim($sql), ',');
$sql .= ' FROM ' . $table;
if (!empty($where)) {
$sql .= ' WHERE ' . $where;
}
if (!empty($order)) {
$sql .= ' order by ' . $order;
}
if ($pageSize>0){
$sql .= ' LIMIT ' . $offset.",".$pageSize;
}
$sql .=";";
// die("查询这里先暂停,看看sql语句 需要往下运行去掉die ".$sql);
$this->stmt = $this->Pre($sql);
if ($this->stmt->execute()) {
if ($this->stmt->rowCount() > 0) {
$this->stmt->setFetchMode(PDO::FETCH_ASSOC);
//返回一个二维数组
return $this->stmt->fetchAll();
}
}
}
/医院
* Delete
* @param $table
* @param $where
* @return bool|string
*/
public function Delete($table, $where)
{
$sql = "DELETE FROM {$table} WHERE ";
if (empty($where)) {
return false;
} else {
$sql .= $where . ";";
}
$this->stmt = $this->Pre($sql);
if ($this->stmt->execute()) {
if ($this->stmt->rowCount() > 0) {
return "删除" . $this->stmt->rowCount() . "条记录";
} else {
return "删除" . $this->stmt->rowCount() . "条记录";
}
} else {
return "删除失败";
}
}
/医院
* prepare
* @param $sql
* @return PDOStatement
*/
public function Pre($sql)
{
$this->stmt = $this->pdo->prepare($sql);
return $this->stmt;
}
}点击 "运行实例" 按钮查看在线实例
<?php
/医院
* Created by PhpStorm.
* User: Administrator
* Date: 2018/4/28 0028
* Time: 下午 2:48
*/
/医院
* 获取总页数
* @param $resultCount
* @param $pageSize
* @return float
*/
function getPageCount($resultCount,$pageSize){
$rowCount = count($resultCount,$pageSize);
$pageCount = ceil($rowCount / $pageSize);
return $pageCount;
}
/医院
* 获取偏移量
* @param $pageNow
* @param $pageSize
* @return float|int
*/
function getOffset($pageNow,$pageSize){
$offset = ($pageNow - 1) * $pageSize;
return $offset;
}点击 "运行实例" 按钮查看在线实例
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>分页</title>
<style type="text/css">
table, th, td {
border: 1px solid black;
}
table th {
background-color: lightskyblue;
}
table {
width: 70%;
margin: 30px auto;
text-align: center;
border-collapse: collapse;
}
h3 {
text-align: center;
}
h3 a {
text-decoration: none;
margin-left: 5px;
border: 1px solid black;
display: inline-block;
height: 30px;
min-width: 15px;
padding: 5px;
background-color: lightgreen;
}
h3 a:hover, .active {
background-color: red;
color: white;
}
form {
display: inline;
}
input {
width: 50px;
}
</style>
</head>
<body>
<?php
require_once "common/localHost.php";
require_once "library/PdoUtil.php";
require_once "Utils/EveryPageUtil.php";
$pdo = new PdoUtil($dsn, $dbname, $dbpass);
$pageNow = isset($_GET['pageNow']) ? $_GET['pageNow'] : 1;
$table = "emp";
$files = "*";
$where = null;
$order = null;
$offset = 0;
/医院
* 代码中直接写
*/
//$resultCount = $pdo->Select($table, $files, $where, $order, 0, 0);//获取一个二维数组
//$rowCount = count($resultCount);
//$pageCount = ceil($rowCount / $pageSize);
////echo "总条数" . $rowCount . "总页数" . $pageCount;
//$offset = ($pageNow - 1) * $pageSize;
////echo "偏移量" . $offset;
//$result = $pdo->Select($table, $files, $where, $order, $offset, $pageSize);//获取一个二维数组
/医院
* 封装起来获取
*/
$resultCount = $pdo->Select($table, $files, $where, $order, 0, 0);//获取一个总二维数组
$pageCount = getPageCount($resultCount, $pageSize);
$offset = getOffset($pageNow, $pageSize);
$result = $pdo->Select($table, $files, $where, $order, $offset, $pageSize);//获取一个单页二维数组
$pdo = null;
?>
<table>
<caption><h3>员工信息表</h3></caption>
<tr>
<th>ID</th>
<th>姓名</th>
<th>等级</th>
<th>邮箱</th>
<th>工资</th>
</tr>
<?php foreach ($result as $data): ?>
<tr>
<td><?php echo $data['id'] ?></td>
<td><?php echo $data['name'] ?></td>
<td><?php echo $data['grade'] ?></td>
<td><?php echo $data['email'] ?></td>
<td><?php echo $data['salary'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<h3>
<?php if ($pageNow != 1): ?>
<a href="http://localhost:63342/WWW/php/0427/0427-self/index.php?pageNow=1">首页</a>
<a href="http://localhost:63342/WWW/php/0427/0427-self/index.php?pageNow=<?php echo $pageNow - 1; ?> ">上一页</a>
<?php endif; ?>
<?php $start = floor(($pageNow - 1) / $pageSize) * $pageSize + 1;
$index = $start; ?>
<?php for (; $start < $index + $pageSize; $start++): ?>
<?php if ($start <= $pageCount): ?>
<a class="<?php if ($_GET['pageNow'] == $start) {
echo 'active';
} ?>"
href="http://localhost:63342/WWW/php/0427/0427-self/index.php?pageNow=<?php echo $start ?>"><?php echo $start ?></a>
<?php endif; ?>
<?php endfor; ?>
<?php if ($pageNow != $pageCount): ?>
<a href=http://localhost:63342/WWW/php/0427/0427-self/index.php?pageNow=<?php echo($pageNow + 1); ?> ">下一页</a>
<a href="http://localhost:63342/WWW/php/0427/0427-self/index.php?pageNow=<?php echo $pageCount ?> ">尾页</a>
<?php endif; ?>
<!--实现页面的快速跳转-->
<form action="http://localhost:63342/WWW/php/0427/0427-self/index.php" method="get">
跳转到
<input type="text" name="pageNow" value="<?php echo $pageNow; ?>">/
<text type="text"><?php echo $pageCount ?>页</text>
<button>跳转</button>
</form>
</h3>
</body>
</html>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号