
批改狀態(tài):合格
老師批語:分頁功能里面有許多小技巧的
try
{
// 連接數(shù)據(jù)庫
$pdo = new PDO('mysql:host=localhost;dbname=tp5;charset=utf8','root','root');
}catch(PDOException $e){
echo $e->getMessage();
}catch(Throwable $e){
echo $e->getMessage();
}
查詢分頁數(shù)據(jù)文件 dataGet.php
<?php
// 引進(jìn)數(shù)據(jù)庫連接
require 'connect.php';
// 獲取分頁數(shù)據(jù)
// 1.每頁顯示的數(shù)量
$num = 5;
// 2.當(dāng)前頁碼,默認(rèn)為1
$page = $_GET['p'] ?? 1;
// 3.計算每一頁的第一條記錄的顯示偏移量
$offset = ($page-1) * $num;
// 4.獲取分頁數(shù)據(jù)
$sql = "SELECT * FROM `tp_user` LIMIT {$num} OFFSET {$offset}";
$users = $pdo->query($sql)->fetchAll();
print_r($users);
echo '<hr>';
// 計算總頁數(shù)
// 計算表中共有多少條記錄
// 每一頁顯示幾條
// 總頁數(shù) = ceil(記錄總數(shù) / 每頁的記錄數(shù))
$sql1 = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `tp_user`";
// 計算總頁數(shù)
$pages = $pdo->query($sql1)->fetch()['total'];
echo $pages;
數(shù)據(jù)顯示
先引進(jìn)查詢數(shù)據(jù)文件
<?php require 'dataGet.php' ?>
把查詢到的數(shù)據(jù)渲染在table表格中
<!-- 表格 -->
<table>
<caption>用戶信息表</caption>
<thead>
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<?php foreach($users as $user): ?>
<tr>
<td><?=$user['id']?></td>
<td><?=$user['username']?></td>
<td><?=$user['age']?></td>
<td><button>編輯</button><button>刪除</button></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
添加分頁欄
<!-- 分頁欄 -->
<!-- 分頁條的動態(tài)生成 -->
<!-- 跳轉(zhuǎn)地址的動態(tài)生成 -->
<!-- 當(dāng)前頁碼的高亮顯示 -->
<?php for($i=1;$i<=$pages;$i++): ?>
<?php
$jump = sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);
// echo $jump;
// $i: 分頁中的頁碼
// $page: 在URL中通過GET獲取的頁碼?p=x
$active = ($i == $page) ? 'active' : null;
?>
<a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a>
<?php endfor ?>
分頁功能完善
添加上一頁以及下一頁等分界
<p>
<!-- 上一頁:解決頁面的越界 -->
<?php
// 獲取前一頁的頁碼
$prev = $page - 1;
if($page == 1) $prev = 1;
?>
<?php if($page!=1):?>
<a href="<?=$_SERVER['PHP_SELF'].'?p=1'?>">首頁</a>
<a href="<?=$_SERVER['PHP_SELF'].'?p='.$prev?>">上一頁</a>
<?php endif ?>
<!-- 正常界面數(shù) -->
<?php for($i=1;$i<=$pages;$i++): ?>
<?php
$jump = sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);
$active = ($i == $page) ? 'active' : null;
?>
<a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a>
<?php endfor ?>
<!-- 下一頁:解決頁面的越界 -->
<?php
// 獲取下一頁的頁碼
$next = $page + 1;
if($next>=$pages) $next = $pages;
?>
<?php if($page!=$pages): ?>
<a href="<?=$_SERVER['PHP_SELF'].'?p='.$next?>">下一頁</a>
<a href="<?=$_SERVER['PHP_SELF'].'?p='.$pages?>">尾頁</a>
<?php endif ?>
</p>
編輯用戶信息
編輯操作
// 獲取操作
$action = $_GET['action'];
$id = $_GET['id'];
switch($action){
// 1:渲染編輯菜單
case 'edit':
// 加載,渲染數(shù)據(jù)編輯菜單
include 'edit.php';
break;
// 2.執(zhí)行編輯操作
case 'doedit':
// 更新
$sql = "UPDATE `tp_user` SET `username`=?,`age`=? WHERE `id`=?";
$stmt = $pdo->prepare($sql);
// 新的數(shù)據(jù)在$_POST
if(!empty($_POST)){
$stmt->execute([$_POST['username'],$_POST['age'],$id]);
if($stmt->rowCount() == 1){
echo '<script>alert("更新成功");location.href="demo2.php"</script>';
}
}
break;
}
編輯頁面
<?php
// 獲取要被編輯的數(shù)據(jù)
$user = $pdo->query("SELECT * FROM `tp_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="username">用戶名:</label>
<input type="text" name="username" id="username" value="<?=$user['username']?>">
</p>
<p>
<label for="age">年齡:</label>
<input type="age" name="age" id="age" value="<?=$user['age']?>">
</p>
<p>
<button>保存</button>
</p>
</form>
</body>
</html>
編輯跳轉(zhuǎn)
<tbody>
<?php foreach($users as $user): ?>
<tr>
<td><?=$user['id']?></td>
<td><?=$user['username']?></td>
<td><?=$user['age']?></td>
<td>
<button onclick="location.href='handle.php?action=edit&id=<?=$user['id']?>'">編輯</button>
<button>刪除</button>
</td>
</tr>
<?php endforeach ?>
</tbody>
編輯前
編輯中
編輯成功后
刪除用戶信息
刪除入口
<button onclick="location.href='handle.php?action=del&id=<?=$user['id']?>'">刪除</button>
刪除操作
// 3.執(zhí)行刪除操作
case 'del':
// 刪除
$sql = "DELETE FROM `tp_user` WHERE `id`=?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id]);
if($stmt->rowCount() == 1) echo '<script>alert("刪除成功");location.href="demo2.php"</script>';
break;
刪除前
刪除中
刪除成功后
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號