
批改狀態(tài):合格
老師批語:現(xiàn)在體會(huì)到了$_SERVER變量的強(qiáng)大作用吧, 里面的每一個(gè)鍵都值得反復(fù)研究, 許多功能 都要靠它來實(shí)現(xiàn)
1、代碼(數(shù)據(jù)連接查詢略)
<?php
$staffs=require 'contect.php';
// echo $pages;
// print_r($staffs);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>員工信息表</title>
<style>
body>div {
width: 700px;
margin: 0 auto;
outline: 1px solid red;
display: flex;
flex-flow: column nowrap;
align-items: center;
}
p {
height: 25px;
display: flex;
justify-content: flex-start;
align-items: center;
}
p a {
text-decoration: none;
width: 38px;
text-align: center;
margin: 0 3px;
}
.active {
background-color: tomato;
color: white;
}
</style>
</head>
<body>
<div>
<table border=1 cellspacing=0 cellpadding=10>
<caption style="font-size:28px">員工信息表</caption>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>職位</th>
<th>電話</th>
<th>操作</th>
</tr>
<?php foreach($staffs as $staff):?>
<tr>
<td><?php echo $staff['id'];?></td>
<td><?php echo $staff['name'];?></td>
<td><?php echo $staff['age'];?></td>
<td><?php echo $staff['sex'] ? '男' : '女';?></td>
<td><?php echo $staff['position'];?></td>
<td><?php echo $staff['mobile'];?></td>
<td><button>編輯</button><button>刪除</button></td>
</tr>
<?php endforeach; ?>
</table>
<p>
<a href="demo.php?p=1">首頁</a>
<?php
$current=filter_input(INPUT_GET,'p',FILTER_VALIDATE_INT);
// echo $current;
if($current<=1){
$prev=1;
}elseif($current>$pages){
$prev=$pages;
}
else{
$prev=$current-1;
}
if($current>=$pages){
$next=$pages;
}elseif($current<1){
$next=1;
}
else{
$next=$current+1;
}
?>
<a href="demo.php?p=<?php echo $prev ?>">前一頁</a>
<?php for($i=1;$i<=$pages;$i++): ?>
<a href="demo.php?p=<?php echo $i;?>"
class='page <?php echo ($i===$current) ? 'active':null ?>'><?php echo $i;?></a>
<?php endfor; ?>
<a href="demo.php?p=<?php echo $next?>">下一頁</a>
<a href="demo.php?p=<?php echo $pages?>">尾頁</a>
</p>
<form action="demo.php" method='GET'>
<input type="number" name="p" min='1' max='<?php echo $pages ?>'>
<button>跳轉(zhuǎn)</button>
</form>
<p>
<a href="demo.php?p=1">首頁</a>
<?php
$current=filter_input(INPUT_GET,'p',FILTER_VALIDATE_INT);
// echo $current;
if($current<=1){
$prev=1;
}elseif($current>$pages){
$prev=$pages;
}
else{
$prev=$current-1;
}
if($current>=$pages){
$next=$pages;
}elseif($current<1){
$next=1;
}
else{
$next=$current+1;
}
?>
<a href="demo.php?p=<?php echo $prev ?>">前一頁</a>
<?php $showpages=5; $offset=($showpages-1)/2;
if($current>$offset+1){
echo "<a href=''>……</a>";
$showstart=$current-$offset;
$showend=$current+$offset;
}
else{
$showstart=1;
$showend=$current+($offset+($offset-$current)+$showstart);
}
if($showend>$pages){
$showstart=$current-($offset+($showend-$pages));
$showend=$pages;
}
?>
<?php for($i=$showstart;$i<=$showend;$i++): ?>
<a href="demo.php?p=<?php echo $i;?>"
class='page <?php echo ($i===$current) ? 'active':null ?>'><?php echo $i;?></a>
<?php endfor; ?>
<?php if($showend<$pages){ echo "<a href=''>……</a>" ; } ?>
<a href="demo.php?p=<?php echo $next?>">下一頁</a>
<a href="demo.php?p=<?php echo $pages?>">尾頁</a>
</p>
</div>
</body>
</html>
2、演示結(jié)果:
1、數(shù)據(jù)庫分頁查詢:SELECT * FROM
staffsLIMIT 5 OFFSET 0
;
(LIMIT 每頁顯示的記錄數(shù),OFFSET 偏移量=每頁顯示的數(shù)量 * (當(dāng)前頁數(shù) - 1))
2、數(shù)據(jù)統(tǒng)計(jì)總數(shù):SELECT COUNT('id') AS sum FROM `staffs
;
3、跳轉(zhuǎn)頁碼:利用form中method=”GET”;設(shè)置input的name=”p”;即可實(shí)現(xiàn)跳轉(zhuǎn)至輸入頁碼;利用input中(min|max)來限制頁碼超出范圍
4、利用showpages頁碼、開始頁碼、結(jié)束頁碼、和頁碼偏移量來結(jié)算顯示省率號(hào);
5、獲取當(dāng)前腳本地址:$_SERVER['PHP_SELF']
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)