Admin.class.php
<?php//它的一個對象執行個體就表示admin表中的一條記錄class Admin {private $id;private $name;private $password;/** * @return the $id */public function getId() {return $this->id;}/** * @return the $name */public function getName() {return $this->name;}/** * @return the $password */public function getPassword() {return $this->password;}/** * @param $id the $id to set */public function setId($id) {$this->id = $id;}/** * @param $name the $name to set */public function setName($name) {$this->name = $name;}/** * @param $password the $password to set */public function setPassword($password) {$this->password = $password;}}?>
AdminService.class.php
<?php/** * 該類是一個商務邏輯處理類 * 完成對admin表的操作 */require_once 'SqlHelper.class.php';class AdminService {//驗證使用者是否合法的方法public function checkAdmin($id, $password) {$sql = "select password,name from admin where id=$id";//建立一個sqlhelper對象$sqlHelper=new SqlHelper();$res=$sqlHelper->execute_dql($sql);if($row=mysql_fetch_assoc($res)){//判斷密碼是否正確if(md5($password)==$row['password']){return $row['name'];}else{//""為假return "";}}//資源釋放mysql_free_result($res);//關閉串連$sqlHelper->close_connect();return false;}}?>
Emp.class.php
<?php class Emp{ }?>
empList.php
<html><head><meta content="text/html;charset=utf-8" http-equiv="content-type"><title>僱員資訊列表</title></head><?phprequire_once 'EmpService.class.php';require_once 'FenyePage.php';//建立一個fenyePage對象執行個體$fenyePage = new FenyePage ();$fenyePage->pageNow = 1;$fenyePage->pageSize = 6;//根據使用者的點擊收取$PageNow的值if (! empty ( $_GET ['pageNow'] )) {$fenyePage->pageNow = $_GET ['pageNow'];}//建立對象執行個體$empService = new EmpService ();$empService->getFenyePage ( $fenyePage );echo "<h1>僱員資訊列表</h1>";//表格顯示分頁查詢後的結果echo "<table width='700px' border='1px' bordercolor='green' cellspacing='0px'>";echo "<tr><th>id</th><th>name</th><th>grade</th>";echo "<th>email</th><th>salary</th><th>刪除</th><th>修改</th></tr>";for($i = 0; $i < count ( $fenyePage->res_array ); $i ++) { $row=$fenyePage->res_array[$i];echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td><td>{$row['grade']}</td>";echo "<td>{$row['email']}</td><td>{$row['salary']}</td>";echo "<td><a href='#'>刪除使用者</a></td>";echo "<td><a href='#'>修改使用者</a></td><tr>";}echo "</table>";//顯示上一頁和下一頁echo $fenyePage->navigate;/*//使用for迴圈列印超連結$page_whole = 5;$start = floor ( ($fenyePage->pageNow - 1) / $page_whole ) * $page_whole + 1;$index = $start;//整體向前翻10頁//如果當前pageNow在1-10,沒有向前翻動的超連結if ($pageNow > $page_whole) {echo " <a href='empList.php?pageNow=" . ($start - 1) . "'> << </a>";}for(; $start < $index + $page_whole; $start ++) {echo "<a href='empList.php?pageNow=$start'>[$start]</a>";}//整體向後10頁翻動echo " <a href='empList.php?pageNow=$start'> >> </a>";//顯示當前頁和共有多少頁echo "當前頁{$pageNow}/共{$pageCount}頁";echo "<br/><br/>";*/?> <form action="empList.php" method="get">跳轉到:<input type="text"name="pageNow" style="width: 40px;" />頁 <input type="submit" value="Go" /></form></html>
empManage.php
<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"></head><?phpecho "歡迎你," . $_GET ['name'] . "登入成功!...";echo "<br/><a href='login.php'>返回重新登入</a>";?><h1>主介面</h1><a href="empList.php">系統管理使用者</a><br /><a href="#">添加使用者</a><br /><a href="#">查詢使用者</a><br /><a href="#">退出系統</a><br /></html>
EmpService.class.php
<?phprequire_once 'SqlHelper.class.php';class EmpService {//一個函數,可以用來擷取公有多少頁function getPageCount($pageSize) {//需要查詢到$rowCount$sql = "select count(id) from emp";$sqlHelper = new SqlHelper ();$res = $sqlHelper->execute_dql ( $sql );//這樣就可以計算pageCountif ($row = mysql_fetch_row ( $res )) {$pageCount = ceil ( $row [0] / $pageSize );}//釋放資源,關閉串連mysql_free_result ( $res );$sqlHelper->close_connect ();return $pageCount;}//一個函數,用來擷取應當顯示的僱員資訊function getEmpListByPage($pageNow, $pageSize) {$sql = "select * from emp limit " . ($pageNow - 1) * $pageSize . ",$pageSize";$sqlHelper = new SqlHelper ();$res = $sqlHelper->execute_dql2 ( $sql );//關閉串連$sqlHelper->close_connect ();return $res;}//第二種使用封裝的方式完成分頁function getFenyePage($fenyePage) {//建立一個SqLHelper對象執行個體$sqlHelper = new SqlHelper ();$sql1 = "select * from emp limit " . ($fenyePage->pageNow - 1) * $fenyePage->pageSize . "," . $fenyePage->pageSize;$sql2 = "select count(id) from emp";$sqlHelper->execute_dql_fenye ( $sql1, $sql2, $fenyePage );$sqlHelper->close_connect();}}?>
FenyePage.php
<?php//這是一個用於分頁資訊的類class FenyePage{public $pageSize=6;public $res_array;//這是顯示資料public $rowCount;//這是從資料庫中擷取public $pageNow;//使用者指定的public $pageCount;//這個是計算得到的總頁數public $navigate;//分頁導航}?>
login.php
<html><head><meta content="text/html;charset=utf-8" http-equiv="content-type"></head><h1>管理員登入系統</h1><form action="loginProcess.php" method="post"><table><tr><td>使用者id</td><td><input type="text" name="id" /></td></tr><tr><td>密 碼</td><td><input type="password" name="password" /></td></tr><tr><td><input type="submit" value="使用者登入" /></td><td><input type="reset" value="重新填寫" /></td></tr></table></form><?phpif (! empty ( $_GET ['errno'] )) {$errno = $_GET ['errno'];if ($errno == 1) {echo "<font color='red' size='3'>你的使用者名稱或密碼錯誤</font>";}}?></html>
loginProcess.php
<?phprequire_once 'AdminService.class.php';//接受使用者的資料//1.id$id = $_POST ['id'];//2.密碼$password = $_POST ['password'];//執行個體化一個AdminService方法$adminService = new AdminService ();if ($adminService->checkAdmin ( $id, $password )) {$name=$adminService->checkAdmin ( $id, $password );//合法header ( "Location:empManage.php?name=$name" );exit ();} else {//非法header ( "Location:login.php?errno=1" );exit ();}?>
SqlHelper.class.php
<?php/** * 操作資料庫的工具類 * 作用是完成對資料庫的操作 * @author Administrator * */class SqlHelper {public $conn; //資料庫連接public $dbname = "test"; //資料庫的名稱public $username = "root"; //public $password = "root";public $host = "localhost";//建構函式public function __construct() {$this->conn = mysql_connect ( $this->host, $this->username, $this->password );if (! $this->conn) {die ( "串連失敗!" . mysql_error () );}mysql_select_db ( $this->dbname, $this->conn );}//執行dql語句public function execute_dql($sql) {$res = mysql_query ( $sql, $this->conn ) or die ( mysql_error () );return $res;}//執行dql語句,但是返回的是一個數組public function execute_dql2($sql) {$arr = array ();$res = mysql_query ( $sql, $this->conn ) or die ( mysql_error () );//把$res中的資源放到數組中區while ( $row = mysql_fetch_assoc ( $res ) ) {$arr [] = $row;}//這裡可以立即關閉$resmysql_free_result ( $res );return $arr;}//考慮分頁情況的查詢//sql1="select * from where limit 0,6";//sql2="select count(id) from ";public function execute_dql_fenye($sql1, $sql2, $fenyePage) {//這裡查詢到了要分頁顯示的資料$res = mysql_query ( $sql1, $this->conn ) or die ( mysql_error () );$arr = array ();//把$res轉移到$arrwhile ( $row = mysql_fetch_assoc ( $res ) ) {$arr [] = $row;}mysql_free_result ( $res );$res2 = mysql_query ( $sql2, $this->conn ) or die ( mysql_error () );if ($row = mysql_fetch_row ( $res2 )) {$fenyePage->pageCount = ceil ( $row [0] / $fenyePage->pageSize );$fenyePage->rowCount = $row [0];}mysql_free_result ( $res2 );//把導航資訊也封裝到fenyePage對象中$navigate="";if ($fenyePage->pageNow > 1) {$prePage = $fenyePage->pageNow - 1;$navigate= "<a href='empList.php? pageNow=$prePage'>上一頁</a> ";}if ($fenyePage->pageNow < $fenyePage->pageCount) {$nextPage = $fenyePage->pageNow + 1;$navigate.= "<a href='empList.php? pageNow=$nextPage'>下一頁</a> ";}//把$arr賦值給$fenyePage$fenyePage->res_array = $arr;$fenyePage->navigate=$navigate;}//執行dml語句public function execute_dml($sql) {$b = mysql_query ( $sql, $this->conn );if (! $b) {return 0; //表示失敗} else {if (mysql_affected_rows ( $this->conn ) > 0) {return 1; //表示執行ok} else {return 2; //表示沒有受影響的行}}}//關閉串連的方法public function close_connect() {if (! empty ( $this->conn )) {mysql_close ( $this->conn );}}}?>
項目目錄結構:
運行效果: