
批改狀態(tài):合格
老師批語:同在這個小后臺看上去像點樣了, 不是嗎?
http://xuanransoftware.com/phpStudy/0513/
<?php
class PageList
{
public static $page;//當前要顯示第幾頁
public static $pageNum = 10;//一頁需要顯示幾條數據
public static $pageSum;//共有幾頁數據要顯示
public static $offset;//查詢記錄的偏移量,select中的offest參數
public static $prevPage;//上一頁
public static $nextPage;//下一頁
public static $startPage;//頁碼開始的頁數
public static $endPage;//頁碼結束的頁數
public static $offsetPage;//頁碼的偏移的數量
public static $showPages = 5;//一共顯示的頁碼數
public static $startOmit;//頁碼省略的開始標記
public static $endOmit;//頁碼省略的結束標記
public static $res;//數據庫中表的結果集
public function __construct($res,$page)
{
//獲取數據庫表中所有的結果集
PageList::$res = $res;
//總頁數
PageList::$pageSum = ceil(count(PageList::$res)/PageList::$pageNum);
//獲取當前顯示的是第幾頁
//如果當前頁碼<1,當前頁為第一頁
if($page<1):
$page = 1;
endif;
//如果當前頁>總頁數,當前面為最后一頁
if($page>PageList::$pageSum):
$page = PageList::$pageSum;
endif;
PageList::$page = $page;
//查詢記錄的偏移量,select中的offest參數
PageList::$offset = (PageList::$page-1)*PageList::$pageNum;
//上一頁
$prev = (PageList::$page)-1;
PageList::$prevPage = ($prev<1) ? 1 : $prev;
//下一頁
$next = PageList::$page+1;
PageList::$nextPage = ($next>PageList::$pageSum) ? (PageList::$pageSum) : $next;
//頁碼的偏移量
PageList::$offsetPage = (PageList::$showPages-1)/2;
//頁碼開始和結束頁數,頁碼開始和結束的省略標記
PageList::$startPage = 1;
PageList::$endPage = PageList::$pageSum;
PageList::$startOmit = null;
PageList::$endOmit = null;
//如果(總頁數>顯示的頁碼數量)則 設置開始結束頁碼和開始結束省略標記
if(PageList::$pageSum>PageList::$showPages):
// 如果(當前頁>頁碼偏移量+1) 則 顯示 開始省略標記
if((PageList::$page)>(PageList::$offsetPage+1)):
PageList::$startOmit = '...';
endif;
// 重置開始和結束的頁碼
//如果(當前頁>頁碼偏移量)
if(PageList::$page>PageList::$offsetPage):
//起始頁碼=當前頁-頁碼偏移量
PageList::$startPage = PageList::$page - PageList::$offsetPage;
//結束頁碼=當前面+頁碼偏移量
PageList::$endPage = PageList::$page + PageList::$offsetPage;
//如果(結束頁碼>總頁數),則結束頁碼=總頁數
if(PageList::$endPage>PageList::$pageSum) :
PageList::$endPage = PageList::$pageSum;
endif;
else:
//如果(當前頁<頁碼偏移量),則 起始頁碼=第一頁 結束頁碼=顯示的頁碼數量
PageList::$startPage = 1;
PageList::$endPage = PageList::$showPages;
endif;
// 如果(當前頁 + 偏移量) > 總頁數
if((PageList::$page+PageList::$offsetPage)>(PageList::$pageSum)):
// 原理, 就是向當前頁前面進行借位
//(新的起始頁碼)=現在的起始頁碼-(當前頁+頁碼偏移量-現在的結束頁碼)
PageList::$startPage = PageList::$startPage-(PageList::$page+PageList::$offsetPage-PageList::$endPage);
endif;
// 如果 (顯示頁碼數量<總頁數)&&((當前頁+頁碼偏移量)<總頁數) 顯示結束省略標記
if((PageList::$showPages<PageList::$pageSum)&&((PageList::$page+PageList::$offsetPage)<PageList::$pageSum)):
PageList::$endOmit = '...';
endif;
endif;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/select.css">
<title>商品查詢頁</title>
</head>
<body>
<div class="show">
<div class="row">
<div>商品編號</div>
<div>商品名稱</div>
<div>商品價格</div>
<div>上架時間</div>
</div>
<?php
require 'autoLoad.php';
use compotents\conn\DBconn;
//連接數據庫,查詢商品表中數據
$conn = new DBconn();
$table = 'tb_goods';//表名
$records = $conn->select($table);//查詢表中所有數據
require 'pageList.php';
$page = $_GET['page'] ?? 1;//設置當前訪問的頁數,默認是第一頁
$pageList = new PageList($records,$page);
$records = $conn->select($table,'*','*',$pageList::$pageNum,$pageList::$offset);//查詢表中當頁數據
foreach($records as $res):
?>
<div class="row">
<div><?php echo $res['id']; ?></div>
<div><?php echo $res['name']; ?></div>
<div><?php echo $res['price'],'元/',$res['unit']; ?></div>
<div><?php echo $res['sdate']; ?></div>
</div>
<?php
endforeach;
?>
<!-- 分頁條 -->
<div class="pagelist">
<!-- 上一頁 -->
<a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $pageList::$prevPage ?>">上一頁</a>
<?php
$self = $_SERVER['PHP_SELF'];
//開始省略標記
if(isset($pageList::$startOmit)):
echo "<a href='{$self}?page=1'>1</a>";
// 如果起始頁就是第2頁,則不顯示第二頁
if($pageList::$startPage !== 2):
echo "<a href='{$self}?page=2'>2</a>";
endif;
echo '<a href="">...</a>';
endif;
//中間顯示的頁碼數
for ($i = $pageList::$startPage;$i<=$pageList::$endPage;$i++):
if( $i ==$pageList::$page):
echo "<a href='{$self}?page={$i}' class='pageActive'>{$i}</a>";
else:
echo "<a href='{$self}?page={$i}'>{$i}</a>";
endif;
endfor;
//結束省略標記
if(isset($pageList::$endOmit)):
$sumPrev =intval($pageList::$pageSum-1) ;//總頁數的上一面
echo '<a href="">...</a>';
if($pageList::$endPage !== $sumPrev):
echo "<a href='{$self}?page={$sumPrev}'>{$sumPrev}</a>";
endif;
echo "<a href='{$self}?page={$pageList::$pageSum}'>{$pageList::$pageSum}</a>";
endif;
?>
<!-- 下一頁 -->
<a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $pageList::$nextPage ?>">下一頁</a>
<form action="<?php echo $self; ?>" method="GET">
<input type="number" name="page" id="page" value="1">
<button type="submit">GO</button>
</form>
</div>
</div>
</body>
</html>
<?php
session_start();
if(!(isset($_SESSION['userNc']))):
exit('<script>alert("您還未登錄,請登錄后操作");location.href="session/login.php";</script>');
endif;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/select.css">
<title>商品修改頁</title>
</head>
<body>
<div class="show">
<div class="row">
<div>商品編號</div>
<div>商品名稱</div>
<div>價格/單位</div>
<div>上架時間</div>
<div>更新操作</div>
</div>
<?php
require 'autoLoad.php';
use compotents\conn\DBconn;
//連接數據庫,查詢商品表中數據
$conn = new DBconn();
$table = 'tb_goods';//表名
$records = $conn->select($table);//查詢表中所有數據
require 'pageList.php';
$page = $_GET['page'] ?? 1;//設置當前訪問的頁數,默認是第一頁
$pageList = new PageList($records,$page);
$records = $conn->select($table,'*','*',$pageList::$pageNum,$pageList::$offset);//查詢表中當頁數據
foreach($records as $res):
?>
<div class="row">
<form action="handle.php?action=update&id=<?php echo $res['id']; ?>" method="POST">
<div style="width:100px"><?php echo $res['id']; ?></div>
<input type="text" name="goodsName" id="goodsName" style="width:120px"
value="<?php echo $res['name']; ?>">
<div>
<input type="text" name="goodsPrice" id="goodsPrice" style="width:50px"
value="<?php echo $res['price']; ?>">/
<input type="text" name="goodsUnit" id="goodsUnit" style="width:50px"
value="<?php echo $res['unit']; ?>">
</div>
<input type="date" name="goodSdate" id="goodSdate" style="width:120px"
value="<?php echo $res['sdate']; ?>">
<div style="width:120px"><button type="submit">修改</button></div>
</form>
</div>
<?php
endforeach;
?>
<!-- 分頁條 -->
<div class="pagelist">
<!-- 上一頁 -->
<a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $pageList::$prevPage ?>">上一頁</a>
<?php
$self = $_SERVER['PHP_SELF'];
//開始省略標記
if(isset($pageList::$startOmit)):
echo "<a href='{$self}?page=1'>1</a>";
// 如果起始頁就是第2頁,則不顯示第二頁
if($pageList::$startPage !== 2):
echo "<a href='{$self}?page=2'>2</a>";
endif;
echo '<a href="">...</a>';
endif;
//中間顯示的頁碼數
for ($i = $pageList::$startPage;$i<=$pageList::$endPage;$i++):
if( $i ==$pageList::$page):
echo "<a href='{$self}?page={$i}' class='pageActive'>{$i}</a>";
else:
echo "<a href='{$self}?page={$i}'>{$i}</a>";
endif;
endfor;
//結束省略標記
if(isset($pageList::$endOmit)):
$sumPrev =intval($pageList::$pageSum-1) ;//總頁數的上一面
echo '<a href="">...</a>';
if($pageList::$endPage !== $sumPrev):
echo "<a href='{$self}?page={$sumPrev}'>{$sumPrev}</a>";
endif;
echo "<a href='{$self}?page={$pageList::$pageSum}'>{$pageList::$pageSum}</a>";
endif;
?>
<!-- 下一頁 -->
<a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $pageList::$nextPage ?>">下一頁</a>
<form action="<?php echo $self; ?>" method="GET">
<input type="number" name="page" id="page" value="1">
<button type="submit">GO</button>
</form>
</div>
</div>
</body>
</html>
<?php
session_start();
if(!(isset($_SESSION['userNc']))):
exit('<script>alert("您還未登錄,請登錄后操作");location.href="session/login.php";</script>');
endif;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/select.css">
<title>商品刪除頁</title>
</head>
<body>
<div class="show">
<div class="row">
<div>商品編號</div>
<div>商品名稱</div>
<div>商品價格</div>
<div>上架時間</div>
<div>刪除操作</div>
</div>
<?php
require 'autoLoad.php';
use compotents\conn\DBconn;
//連接數據庫,查詢商品表中數據
$conn = new DBconn();
$table = 'tb_goods';//表名
$records = $conn->select($table);//查詢表中所有數據
require 'pageList.php';
$page = $_GET['page'] ?? 1;//設置當前訪問的頁數,默認是第一頁
$pageList = new PageList($records,$page);
$records = $conn->select($table,'*','*',$pageList::$pageNum,$pageList::$offset);//查詢表中當頁數據
foreach($records as $res):
?>
<div class="row">
<div><?php echo $res['id']; ?></div>
<div><?php echo $res['name']; ?></div>
<div><?php echo $res['price'],'元/',$res['unit']; ?></div>
<div><?php echo $res['sdate']; ?></div>
<div><a href="handle.php?action=delete&id=<?php echo $res['id']; ?>">刪除</a></div>
</div>
<?php
endforeach;
?>
<!-- 分頁條 -->
<div class="pagelist">
<!-- 上一頁 -->
<a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $pageList::$prevPage ?>">上一頁</a>
<?php
$self = $_SERVER['PHP_SELF'];
//開始省略標記
if(isset($pageList::$startOmit)):
echo "<a href='{$self}?page=1'>1</a>";
// 如果起始頁就是第2頁,則不顯示第二頁
if($pageList::$startPage !== 2):
echo "<a href='{$self}?page=2'>2</a>";
endif;
echo '<a href="">...</a>';
endif;
//中間顯示的頁碼數
for ($i = $pageList::$startPage;$i<=$pageList::$endPage;$i++):
if( $i ==$pageList::$page):
echo "<a href='{$self}?page={$i}' class='pageActive'>{$i}</a>";
else:
echo "<a href='{$self}?page={$i}'>{$i}</a>";
endif;
endfor;
//結束省略標記
if(isset($pageList::$endOmit)):
$sumPrev =intval($pageList::$pageSum-1) ;//總頁數的上一面
echo '<a href="">...</a>';
if($pageList::$endPage !== $sumPrev):
echo "<a href='{$self}?page={$sumPrev}'>{$sumPrev}</a>";
endif;
echo "<a href='{$self}?page={$pageList::$pageSum}'>{$pageList::$pageSum}</a>";
endif;
?>
<!-- 下一頁 -->
<a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $pageList::$nextPage ?>">下一頁</a>
<form action="<?php echo $self; ?>" method="GET">
<input type="number" name="page" id="page" value="1">
<button type="submit">GO</button>
</form>
</div>
</div>
</body>
</html>
<?php
require 'autoLoad.php';
use compotents\conn\DBconn;
$user =new DBconn();
$table = 'tb_goods';//表名
$where =''; //判斷的條件
$data =[];//添加或者更新的數據
$action = $_GET['action'];
switch ($action)
{
case 'insert':
$name = $_POST['goodsName'];
$price = $_POST['goodsPrice'];
$unit = $_POST['goodsUnit'];
$date = $_POST['goodsSdate'];
$data = ['name'=>"$name",'price'=>"$price",'unit'=>"$unit",'sdate'=>"$date"];
$user->insert($table,$data);
break;
case 'update':
$id = $_GET['id'];
$name = $_POST['goodsName'];
$price = $_POST['goodsPrice'];
$unit = $_POST['goodsUnit'];
$sdate = $_POST['goodSdate'];
$where = "`id`=$id";
$data = ['name'=>"$name",'price'=>"$price",'unit'=>"$unit",'sdate'=>"$sdate"];
$user->update($table,$data,$where);
break;
case 'delete':
$id = $_GET['id'];
$where = "`id`=$id";
$user->delete($table,$where);
break;
default:
echo '不支持此操作';
}
?>
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號