這個章節將講解分層模式對雇員管理系統的系統,首先看下基本的流程圖設計:

### 下面是具體的代碼:
1、login.php(參照上節)
2、loginProcess.php
~~~
<?php
/**
*
* @author jsh
* @version
*/
require_once 'AdminService.class.php';
//接受用戶數據
$id=$_POST['id'];
$password=$_POST['password'];
//實例化對象
$adminService=new AdminService();
if(($name=$adminService->checkAdmin($id, $password)) != ""){
header("Location:https://192.168.1.110/myphp/manage/empManage.php?name=$name");
exit();
} else {
header("Location:https://192.168.1.110/myphp/manage/login.php?errno=1");
exit();
}
?>
~~~
3、empmain.php(參照上節)
4、empList.php
~~~
<html>
<head>
<meta http-equiv="content-tpe" content="text/html;charset-utf-8"/>
<title>雇員管理列表</title>
<script type="text/javascript">
<!--
function check(){
return window.confirm("是否要刪除用戶");
}
//-->
</script>
</head>
<?php
include_once 'EmpService.class.php';
include_once 'FenyePage.class.php';
/*
$pageNow :顯示第幾頁:用戶輸入
$pageCount:共有幾頁[]
$rowCount:共有多少條記錄[數據庫獲取]
$pagesize:每頁顯示幾條記錄[人為定義]
*/
if(!empty($_GET['flag'])){
$id=$_GET['Id'];
$empservice=new empService();
$empservice->delUserById($id);
}
if(!empty($_GET['pageNow'])){
$pageNow = $_GET['pageNow'];
} else {
$pageNow = 1;
}
$fenyePage=new fenyepage();
$fenyePage->pageSize = 3;
$fenyePage->pageNow = $pageNow;
$fenyePage->page_num=3;
//獲取共有多少記錄
$empservice=new empService();
$pageCount=$empservice->getFenYePageInfo($fenyePage);
echo "<h1>雇員管理系統</h1>";
echo "<table width='700px' border='1px'>";
echo "<tr><th>Id</th><th>Name</th><th>Grade</th><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><th>{$row['Id']}</th><th>{$row['Name']}</th><th>{$row['Grade']}</th>".
"<th>{$row['Email']}</th><th>{$row['Salary']}</th><th><a onclick='return check()' href='empList.php?flag=1&Id={$row['Id']}'>刪除用戶</a></th>".
"<th><a href='empList.php?pageNow={$row['Id']}'>修改用戶</a></th></tr>";
}
echo "</table>";
echo $fenyePage->navigation_bars;
/* //打印上一頁下一頁
if($fenyePage->pageNow>1){
$prepage = $fenyePage->pageNow - 1;
echo "<a href='empList.php?pageNow=$prepage'>上一頁</a>";
}
if($fenyePage->pageNow<$fenyePage->pageCount){
$nextpage = $fenyePage->pageNow + 1;
echo "<a href='empList.php?pageNow=$nextpage'>下一頁</a>";
}
//翻頁
$start=floor(($fenyePage->pageNow - 1)/$fenyePage->page_num) * $fenyePage->page_num + 1;
$index = $start;
for(;$start < $fenyePage->pageCount && $start<$index + $fenyePage->page_num;$start++){
echo "<a href='empList.php?pageNow=$start'>[$start]</a>";
}
//顯示當前頁和共有多少頁
echo "??當前頁{$fenyePage->pageNow}/共{$fenyePage->pageCount}頁";
*/
?>
<!-- 指定跳轉到某頁 -->
<form action="empList.php" method="get">
跳轉到:<input type="text" name="pageNow"/>
<input type="submit" value="GO"/>
</form>
</html>
~~~
5、AdminService.class.php
~~~
<?php
//該類是一個業務邏輯處理類,
require_once 'SqlHelper.class.php';
class AdminService {
//提供一個驗證用戶是否合法的方法
public function checkAdmin($id,$password){
$sql="select * 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'];
}
}
//釋放資源
mysql_free_result($res);
//關閉鏈接
$sqlHelper->close_connect();
return "";
}
}
?>
~~~
6、empService.class.php
~~~
<?php
require_once 'SqlHelper.class.php';
class empService {
//一個函數可以獲得多少頁
function getPageCount($pagesize){
//需要查詢$rowcount
$sql="select count(Id) from emp";
$sqlHelper=new SqlHelper();
$result=$sqlHelper->execute_dql($sql);
if($row=mysql_fetch_row($result)){
$pageCount=ceil($row[0]/$pagesize);
}
//釋放資源
mysql_free_result($result);
//關閉連接
$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;
}
//分頁
public function getFenYePageInfo($fenyePage){
$sqlHelper=new SqlHelper();
$sql1="select * from emp limit ".($fenyePage->pageNow - 1)*$fenyePage->pageSize.",$fenyePage->pageSize";
$sql2="select count(Id) from emp";
$php_name="empList.php";
$sqlHelper->exectue_dql_fenye($sql1, $sql2, $fenyePage,$php_name);
//關閉鏈接
$sqlHelper->close_connect();
return $fenyePage;
}
//刪除用戶
public function delUserById($id){
$sql="delete from emp where Id='$id'";
$sqlHelper = new SqlHelper();
$res=$sqlHelper->execute_dml($sql);
return $res;
}
}
?>
~~~
7、SqlHelper.class.php
~~~
<?php
//這是一個工具類,作用是完成對數據庫的基本操作
class SqlHelper {
public $conn;
public $dbname="manage";
public $usename="root";
public $password="";
public $host="192.168.1.110:3306";
//構造方法,連接及選擇數據庫
public function __construct(){
$this->conn=mysql_connect($this->host,$this->usename,$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;
}
//省去資源釋放的
public function execute_dql2($sql){
$res=mysql_query($sql,$this->conn) or die("執行失敗".mysql_error());
$arr=array();
$i=0;
while ($row=mysql_fetch_assoc($res)){
$arr[$i++]=$row;
}
//釋放資源
mysql_free_result($res);
return $arr;
}
/* 考慮分頁情況的查詢
$sql1="select count(Id) from 表名";
$sql2="select * from 表名 limit x,y"; */
public function exectue_dql_fenye($sql1,$sql2,&$fenyePage,$php_name){
$navigation_bars="";
$res=mysql_query($sql1,$this->conn) or die("執行失敗".mysql_error());
$arr=array();
$i=0;
while ($row=mysql_fetch_assoc($res)){
$arr[$i++]=$row;
}
//釋放資源
mysql_free_result($res);
//獲得數據庫共有多少行
$res=mysql_query($sql2,$this->conn) or die(mysql_errno());
if($row=mysql_fetch_row($res)){
$fenyePage->row_Count=$row[0];
}
$fenyePage->res_array=$arr;
//共有多少頁
$fenyePage->pageCount = ceil($fenyePage->row_Count/$fenyePage->pageSize);
//釋放資源
mysql_free_result($res);
if($fenyePage->pageNow>1){
$prepage = $fenyePage->pageNow - 1;
$navigation_bars="<a href='$php_name?pageNow=$prepage'>上一頁</a>";
}
if($fenyePage->pageNow<$fenyePage->pageCount){
$nextpage = $fenyePage->pageNow + 1;
$navigation_bars .= "<a href='$php_name?pageNow=$nextpage'>下一頁</a>";
}
//翻頁
$start=floor(($fenyePage->pageNow - 1)/$fenyePage->page_num) * $fenyePage->page_num + 1;
$index = $start;
if($fenyePage->pageNow > $fenyePage->page_num){
$navigation_bars .="<a href='$php_name?pageNow=".($start-1)."'>?<<?</a>";
}
for(;$start < $fenyePage->pageCount && $start<$index + $fenyePage->page_num;$start++){
$navigation_bars .= "<a href='$php_name?pageNow=$start'>[$start]</a>";
}
$navigation_bars .="<a href='$php_name?pageNow=".($start+1)."'>?<<?</a>";
//顯示當前頁和共有多少頁
$navigation_bars .= "??當前頁{$fenyePage->pageNow}/共{$fenyePage->pageCount}頁";
$fenyePage->navigation_bars=$navigation_bars;
}
//執行DML語句 更新 刪除 添加
public function execute_dml($sql){
$b=mysql_query($sql,$this->conn);
if(!$b){
return 0;//失敗
} else {
if(mysql_affected_rows($this->conn)){
return 1;//執行成功
}else{
return 2;//表示沒有行發生變化
}
}
}
//關閉連接的方法
public function close_connect(){
if($this->conn){
mysql_close($this->conn);
}
}
}
?>
~~~
8、fenyepage.class.php
~~~
<?php
class fenyepage{
public $pageSize; //每頁顯示的行數
public $pageNow; //當前頁
public $pageCount; //共有多少頁。計算得到
public $res_array;//顯示數據,數據庫獲得
public $row_Count; //共有多少行,數據庫獲得
public $page_num; //翻頁數
public $navigation_bars;//導航條
}
?>
~~~
下面展示的是一個主要的頁面:

MVC 的基本概念:
1、? MVC是一種軟件的設計模式-》套路
2、? 解釋下每個字母的含義
M(Model模型:處理業務邏輯 比如 各種類
V (View 視圖、界面:PHP編寫的
C (Controller 控制器,主要作用是接受用戶的請求,并調用某個方法,完成任務,跳轉到下一個界面
3、? 核心思想
強制程序員在編寫項目的時候,把數據的輸入、處理、輸出分開。

Mvc 的處理過程:
1、? 首先控制器接受用戶的請求,并決定應該調用那個模型來進行處理
2、? 然后調用模型來處理用戶的請求并返回數據
3、? 最后控制器用相應的視圖顯示模型返回的數據,并通過瀏覽器呈現給用戶。
- 前言
- HTML(第一天)
- HTML(第二天)
- DIV+CSS(第一天)
- DIV+CSS(第二天)
- DIV+CSS(第三天)
- DIV+CSS(第四天)
- PHP開發環境配置說明
- PHP基礎數據類型及運算符介紹
- 走進PHP函數
- 走進位運算
- 走進數組及相關數組函數
- 面向對象的編程(類與對象)
- 面向對象的編程(類成員方法用法)
- 構造方法,析構方法
- static,global用法
- 靜態方法
- 面向對象的編程方法
- 接口方法
- 接口VS繼承、final、const
- 錯誤及異常處理機制、錯誤日志
- HTTP協議深度剖析
- 文件下載
- PHP數據庫編程
- 數據庫編程(2)
- 超全局變量
- Zend studio 開發環境入門
- 雇員管理系統(1)
- 雇員管理系統(2)
- 會話技術(cookie session)
- 會話技術 session場景使用介紹!
- PHP.in中session和cookie的配置說明
- PHP文件編程
- 報表及繪圖技術
- 報表及繪圖技術(jpgraph庫使用,Linux安裝及配置說明)
- XML基本語法及DTD介紹
- XML編程(Dom技術)
- XML編程(Xpath技術,simpleXml技術)基礎入門
- 網上支付平臺PHP版本
- javascript基礎入門