07-php僱員管理系統-分層模式實現登入,分頁

來源:互聯網
上載者:User


項目目錄結構:

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';$pageSize = 20; //每頁數$rowCount = 0; //記錄數$pageNow = 1; //當前頁//根據使用者的點擊收取$PageNow的值if (! empty ( $_GET ['pageNow'] )) {$pageNow = $_GET ['pageNow'];}//建立對象執行個體$empService = new EmpService ();//調用求總共的頁數的方法$pageCount = $empService->getPageCount ( $pageSize );//調用getEmplistBypage方法,來獲得應當顯示的資料$res2 = $empService->getEmpListByPage ( $pageNow, $pageSize );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 ( $res2 ); $i ++) {$row = $res2 [$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>";//顯示上一頁和下一頁if ($pageNow > 1) {$prePage = $pageNow - 1;echo "<a href='empList.php?pageNow=$prePage'>上一頁</a> ";}if ($pageNow < $pageCount) {$nextPage = $pageNow + 1;echo "<a href='empList.php?pageNow=$nextPage'>下一頁</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;}}?>

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 () );$i = 0;//把$res中的資源放到數組中區while ( $row = mysql_fetch_assoc ( $res ) ) {$arr [$i ++] = $row;}//這裡可以立即關閉$resmysql_free_result ( $res );return $arr;}//執行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 );}}}?>

運行效果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.