
批改狀態(tài):合格
老師批語:
namespace one\test\controller {
class DEMO{
public static function test(){
return "one.demo.test";
}
}
// echo DEMO::test();
}
namespace one\test\model {
class DEMO{
public static function test(){
return "TWO.demo.test";
}
}
//直接引用類
//類有重名可以用as給類更名
// use \one\test\controller\DEMO as DE;
// echo DE::test();
//引用命名空間后再引用類,并給controller更名
use \one\test\controller as ctl;
echo ctl\DEMO::test();
}
spl_autoload_register(function($class){
// DIRECTORY_SEPARATOR系統(tǒng)分隔符常量,把所有的\都轉(zhuǎn)為系統(tǒng)分隔符
$file = str_replace('\\',DIRECTORY_SEPARATOR,$class).'.php';
// 判斷$file是否是正常的文件,是否存在該文件,都為真返回引入,為假拋錯(cuò)
if(is_file($file)&&file_exists($file)){
require $file;
}else{
throw new \Exception("文件名不合法或文件不存在");
}
});
mkdir(目錄名稱,權(quán)限(默認(rèn) 0777,即允許全局訪問),true);
chmod(目錄名稱,權(quán)限);
move_uploaded_file(上傳文件的臨時(shí)路徑,新目錄及文件名,)
in_array(要搜索的值,被搜索的數(shù)組,是否全等(true 全等 ,false 非全等,默認(rèn)為 false))
可以用來判斷文件類型是否在我們規(guī)定的類型內(nèi)數(shù)據(jù)庫分頁處理,主要需要捋清楚每頁要分的條數(shù)、每頁顯示條數(shù)的起始位置計(jì)算以及最后有多少頁數(shù)據(jù),判斷起始頁跟結(jié)束頁,對(duì)上一頁、下一頁進(jìn)行處理
<?php
//判斷是否連接數(shù)據(jù)庫
try{
$pdo = new PDO('mysql:host=localhost;dbname=test','root','z1071930401');
}catch(PDOException $e){
echo '數(shù)據(jù)庫連接失敗' . $e->getMessage();
}
// 給p賦初始默認(rèn)值
if(empty($_GET['p'])){
$p = 1;
}else{
$p = $_GET['p'];
}
// 設(shè)置每頁條數(shù)
$count = 5;
//數(shù)據(jù)庫的數(shù)據(jù)下標(biāo)類似于數(shù)組,都是從0開始,因此第一頁數(shù)據(jù)就是0-4,第二頁就是0-5,按照頁數(shù)與條數(shù)的乘積計(jì)算,每頁顯示的條數(shù)應(yīng)該為($p-1)*$count
$sql = 'SELECT * FROM `user` LIMIT ' . ($p-1)*$count . ',' . $count;
$pre = $pdo -> prepare($sql);
$exe = $pre -> execute();
// 將查詢出來的結(jié)果集賦值給$data
$data = $pre -> fetchAll();
//使用count()方法查詢總共有多少條數(shù)據(jù)
$pre = $pdo -> prepare('SELECT count(*) as total FROM `user`');
$exe = $pre -> execute();
// 將查詢出來的條數(shù)賦值給total
$total = $pre -> fetch()['total'];
// 使用ceil()方法向上四舍入為最接近的整數(shù),獲取頁數(shù),擴(kuò)展:floor()方法向下四舍入為最接近的整數(shù),round()對(duì)浮點(diǎn)數(shù)進(jìn)行四舍五入
$page = ceil($total/$count);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用戶分頁</title>
<style>
.pagination {
display: inline-block;
padding-left: 0;
margin: 20px 0;
border-radius: 4px;
}
.pagination > li {
display: inline;
}
.pagination > li > a,
.pagination > li > span {
position: relative;
float: left;
padding: 6px 12px;
margin-left: -1px;
line-height: 1.42857143;
color: #337ab7;
text-decoration: none;
background-color: #fff;
border: 1px solid #ddd;
}
.pagination > li:first-child > a,
.pagination > li:first-child > span {
margin-left: 0;
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
}
.pagination > li:last-child > a,
.pagination > li:last-child > span {
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
}
.pagination > li > a:hover,
.pagination > li > span:hover,
.pagination > li > a:focus,
.pagination > li > span:focus {
z-index: 2;
color: #23527c;
background-color: #eee;
border-color: #ddd;
}
.pagination > .active > a,
.pagination > .active > span,
.pagination > .active > a:hover,
.pagination > .active > span:hover,
.pagination > .active > a:focus,
.pagination > .active > span:focus {
z-index: 3;
color: #fff;
cursor: default;
background-color: #337ab7;
border-color: #337ab7;
}
.pagination > .disabled > span,
.pagination > .disabled > span:hover,
.pagination > .disabled > span:focus,
.pagination > .disabled > a,
.pagination > .disabled > a:hover,
.pagination > .disabled > a:focus {
color: #777;
cursor: not-allowed;
background-color: #fff;
border-color: #ddd;
}
.pagination > form > input,button{
position: relative;
float: left;
padding: 6px 12px;
margin-left: -1px;
line-height: 1.42857143;
color: #337ab7;
text-decoration: none;
background-color: #fff;
border: 1px solid #ddd;
}
.pagination > form > button:hover{
z-index: 2;
cursor:pointer;
color: #23527c;
background-color: #eee;
border-color: #ddd;
}
</style>
</head>
<body>
<a href="add.php">添加</a>
<table border="1">
<thead>
<tr>
<th width="50">ID</th>
<th width="100">賬戶</th>
<th width="100">姓名</th>
<th width="100">年齡</th>
<th width="100">手機(jī)號(hào)</th>
<th width="200">入職時(shí)間</th>
<th width="200">登錄時(shí)間</th>
<th width="100">狀態(tài)</th>
</tr>
</thead>
<tbody>
<?php
// 循環(huán)遍歷顯示的數(shù)據(jù)內(nèi)容
foreach($data as $v){
?>
<tr>
<td><?=$v['id'] ?></td>
<td><?=$v['account'] ?></td>
<td><?=$v['name'] ?></td>
<td><?=$v['age'].'歲' ?></td>
<td><?=$v['phone'] ?></td>
<td><?=date('Y-m-d',$v['add_time']) ?></td>
<td><?=date('Y-m-d H:i:s',$v['last_time']) ?></td>
<!-- <td><?=$v['status']==1?'開啟':'關(guān)閉' ?></td> -->
<td>
<?php
if($v['status'] == 1){
echo '<span style="color:green;">開啟</span>';
}else{
echo '<span style="color:red;">關(guān)閉</span>';
}
?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
// 當(dāng)前頁面為1,就代表沒有上一頁,設(shè)置上一頁無法點(diǎn)擊
$html = '';
if($p == 1){
$html .= '<li class="disabled">';
$html .= ' <a>上一頁</a>';
$html .= '</li>';
}else{
$html .= '<li>';
$html .= ' <a href="list.php?p='.($p-1).'">上一頁</a>';
$html .= '</li>';
}
// 循環(huán)遍歷每頁數(shù)據(jù)鏈接
for($i=1;$i<=$page;$i++){
$html .= '<li ';
// 判斷點(diǎn)擊的是哪一頁給哪一頁添加active類
if($i == $p){
$html .= 'class="active"';
}
$html .= '>';
$html .= ' <a href="list.php?p='.$i.'">'.$i.'</a>';
$html .= '</li>';
}
//判斷如果點(diǎn)擊的頁碼為最后一頁,就代表沒有下一頁,設(shè)置下一頁無法點(diǎn)擊
if($p == $page){
$html .= '<li class="disabled">';
$html .= ' <a>下一頁</a>';
$html .= '</li>';
}else{
$html .= '<li>';
$html .= ' <a href="list.php?p='.($p+1).'">下一頁</a>';
$html .= '</li>';
}
echo $html;
?>
<!-- 配置輸入框加跳轉(zhuǎn)頁 -->
<form action="list.php" method="get" onclick="false">
<input type="text" name="p">
<button onclick="window.location.href=<?php echo '\'list.php?p='.$_GET['p'].'\''; ?>;">跳轉(zhuǎn)</button>
</form>
</ul>
</body>
</html>
微信掃碼
關(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)