雇员系统2

这个章节将讲解分层模式对雇员管理系统的系统,首先看下基本的流程图设计:



下面是具体的代码:

1、login.php(参照上节)
2、loginProcess.php
  1. <?php  
  2. /** 
  3.  * 
  4.  * @author jsh 
  5.  * @version  
  6.  */  
  7.     
  8.     require_once 'AdminService.class.php';  
  9.     //接受用户数据   
  10.     $id=$_POST['id'];  
  11.     $password=$_POST['password'];  
  12.       
  13.     //实例化对象  
  14.     $adminService=new AdminService();  
  15.       
  16.     if(($name=$adminService->checkAdmin($id$password)) != ""){  
  17.             header("Location:https://192.168.1.110/myphp/manage/empManage.php?name=$name");  
  18.             exit();  
  19.     } else {  
  20.         header("Location:https://192.168.1.110/myphp/manage/login.php?errno=1");  
  21.         exit();  
  22.     }      
  23.  ?>  


3、empmain.php(参照上节)
4、empList.php
  1. <html>  
  2. <head>  
  3. <meta http-equiv="content-tpe" content="text/html;charset-utf-8"/>  
  4. <title>雇员管理列表</title>  
  5. <script type="text/javascript">  
  6. <!--  
  7.     function check(){  
  8.        return window.confirm("是否要删除用户");  
  9.     }  
  10. //-->    
  11. </script>  
  12. </head>  
  13.   
  14.   
  15.   
  16. <?php  
  17.    include_once 'EmpService.class.php';     
  18.    include_once 'FenyePage.class.php';  
  19.     /* 
  20.     $pageNow :显示第几页:用户输入 
  21.     $pageCount:共有几页[] 
  22.     $rowCount:共有多少条记录[数据库获取] 
  23.     $pagesize:每页显示几条记录[人为定义] 
  24.     */  
  25.      
  26.    if(!empty($_GET['flag'])){  
  27.        $id=$_GET['Id'];  
  28.        $empservice=new empService();  
  29.        $empservice->delUserById($id);  
  30.    }  
  31.    if(!empty($_GET['pageNow'])){  
  32.     $pageNow = $_GET['pageNow'];  
  33.    } else {  
  34.     $pageNow = 1;  
  35.    }  
  36.      
  37.    $fenyePage=new fenyepage();  
  38.    $fenyePage->pageSize = 3;  
  39.    $fenyePage->pageNow = $pageNow;  
  40.    $fenyePage->page_num=3;  
  41.      
  42.     //获取共有多少记录  
  43.     $empservice=new empService();  
  44.     $pageCount=$empservice->getFenYePageInfo($fenyePage);  
  45.     echo "<h1>雇员管理系统</h1>";  
  46.     echo "<table width='700px' border='1px'>";  
  47.     echo "<tr><th>Id</th><th>Name</th><th>Grade</th><th>Email</th><th>Salary</th><th>删除用户</th><th>修改用户</th></tr>";  
  48.       
  49.       
  50.     for($i=0;$i<count($fenyePage->res_array);$i++){  
  51.         $row=$fenyePage->res_array[$i];  
  52.         echo "<tr><th>{$row['Id']}</th><th>{$row['Name']}</th><th>{$row['Grade']}</th>".  
  53.                 "<th>{$row['Email']}</th><th>{$row['Salary']}</th><th><a οnclick='return check()' href='empList.php?flag=1&Id={$row['Id']}'>删除用户</a></th>".  
  54.                 "<th><a href='empList.php?pageNow={$row['Id']}'>修改用户</a></th></tr>";  
  55.     }  
  56.     echo "</table>";  
  57.       
  58.     echo $fenyePage->navigation_bars;  
  59.   /*   //打印上一页下一页 
  60.     if($fenyePage->pageNow>1){ 
  61.         $prepage = $fenyePage->pageNow - 1; 
  62.         echo "<a href='empList.php?pageNow=$prepage'>上一页</a>"; 
  63.     } 
  64.     if($fenyePage->pageNow<$fenyePage->pageCount){ 
  65.         $nextpage = $fenyePage->pageNow + 1; 
  66.         echo "<a href='empList.php?pageNow=$nextpage'>下一页</a>"; 
  67.     } 
  68.      
  69.     //翻页 
  70.     $start=floor(($fenyePage->pageNow - 1)/$fenyePage->page_num) * $fenyePage->page_num + 1; 
  71.     $index = $start; 
  72.      
  73.     for(;$start < $fenyePage->pageCount && $start<$index + $fenyePage->page_num;$start++){ 
  74.         echo "<a href='empList.php?pageNow=$start'>[$start]</a>"; 
  75.     } 
  76.     //显示当前页和共有多少页 
  77.     echo "  当前页{$fenyePage->pageNow}/共{$fenyePage->pageCount}页"; 
  78.      */  
  79.      
  80. ?>  
  81. <!--     指定跳转到某页 -->  
  82.     <form action="empList.php" method="get">  
  83.       
  84.           跳转到:<input type="text" name="pageNow"/>  
  85.     <input type="submit" value="GO"/>  
  86.     </form>  
  87.       
  88. </html>  

5、AdminService.class.php
  1. <?php  
  2.     //该类是一个业务逻辑处理类,  
  3.     require_once 'SqlHelper.class.php';  
  4.     class AdminService {  
  5.         //提供一个验证用户是否合法的方法  
  6.           
  7.         public function checkAdmin($id,$password){  
  8.             $sql="select * from admin where Id=$id";  
  9.               
  10.             //创建一个SqlHelper对象  
  11.             $sqlHelper=new SqlHelper();  
  12.             //执行查询命令  
  13.             $res=$sqlHelper->execute_dql($sql);  
  14.             if($row=mysql_fetch_assoc($res)){  
  15.                 if(md5($password) == $row['Password']){  
  16.                       
  17.                     return $row['Name'];  
  18.                 }  
  19.             }  
  20.             //释放资源  
  21.             mysql_free_result($res);  
  22.             //关闭链接  
  23.             $sqlHelper->close_connect();  
  24.               
  25.             return "";  
  26.         }  
  27.           
  28.           
  29.     }  
  30.   
  31. ?>  

6、empService.class.php
  1. <?php  
  2.     require_once 'SqlHelper.class.php';  
  3.     class empService {  
  4.              
  5.         //一个函数可以获得多少页  
  6.         function getPageCount($pagesize){  
  7.             //需要查询$rowcount  
  8.             $sql="select count(Id) from emp";  
  9.             $sqlHelper=new SqlHelper();  
  10.             $result=$sqlHelper->execute_dql($sql);  
  11.           
  12.             if($row=mysql_fetch_row($result)){  
  13.                 $pageCount=ceil($row[0]/$pagesize);  
  14.             }  
  15.             //释放资源  
  16.             mysql_free_result($result);  
  17.             //关闭连接  
  18.             $sqlHelper->close_connect();  
  19.             return $pageCount;  
  20.         }  
  21.           
  22.         //获得当前页的雇员信息  
  23.         function getEmpListByPage($pageNow,$pageSize){  
  24.             $sql="select * from emp limit ".($pageNow-1)*$pageSize.",$pageSize";  
  25.                
  26.             $sqlHelper=new SqlHelper();  
  27.             $res=$sqlHelper->execute_dql2($sql);  
  28.               
  29.             //关闭连接  
  30.             $sqlHelper->close_connect();  
  31.           
  32.             return $res;  
  33.         }  
  34.         //分页  
  35.         public function getFenYePageInfo($fenyePage){  
  36.             $sqlHelper=new SqlHelper();  
  37.             $sql1="select * from emp limit ".($fenyePage->pageNow - 1)*$fenyePage->pageSize.",$fenyePage->pageSize";  
  38.             $sql2="select count(Id) from emp";  
  39.             $php_name="empList.php";  
  40.             $sqlHelper->exectue_dql_fenye($sql1$sql2$fenyePage,$php_name);  
  41.             //关闭链接  
  42.             $sqlHelper->close_connect();  
  43.             return $fenyePage;  
  44.               
  45.         }  
  46.           
  47.         //删除用户  
  48.         public function delUserById($id){  
  49.             $sql="delete from emp where Id='$id'";  
  50.             $sqlHelper = new SqlHelper();  
  51.             $res=$sqlHelper->execute_dml($sql);  
  52.             return $res;  
  53.         }  
  54.     }  
  55. ?>  

7、SqlHelper.class.php
  1. <?php  
  2.     //这是一个工具类,作用是完成对数据库的基本操作  
  3.     class SqlHelper {  
  4.         public $conn;  
  5.         public $dbname="manage";  
  6.         public $usename="root";  
  7.         public $password="";  
  8.         public $host="192.168.1.110:3306";  
  9.           
  10.         //构造方法,连接及选择数据库  
  11.         public function __construct(){  
  12.             $this->conn=mysql_connect($this->host,$this->usename,$this->password);  
  13.             if(!$this->conn){  
  14.                 die("连接失败".mysql_error());  
  15.             }  
  16.             mysql_select_db($this->dbname,$this->conn);  
  17.         }  
  18.           
  19.         //执行dql语句  查询  
  20.         public function execute_dql($sql){  
  21.             $res=mysql_query($sql,$this->conn) or die("执行失败".mysql_error());  
  22.             return $res;  
  23.         }  
  24.           
  25.         //省去资源释放的  
  26.         public function execute_dql2($sql){  
  27.             $res=mysql_query($sql,$this->conn) or die("执行失败".mysql_error());  
  28.             $arr=array();  
  29.             $i=0;  
  30.             while ($row=mysql_fetch_assoc($res)){  
  31.                   
  32.                 $arr[$i++]=$row;  
  33.             }  
  34.             //释放资源  
  35.             mysql_free_result($res);  
  36.             return $arr;  
  37.         }  
  38.           
  39.         /*    考虑分页情况的查询 
  40.         $sql1="select count(Id) from 表名"; 
  41.         $sql2="select * from 表名 limit x,y"; */  
  42.         public function exectue_dql_fenye($sql1,$sql2,&$fenyePage,$php_name){  
  43.             $navigation_bars="";  
  44.             $res=mysql_query($sql1,$this->conn) or die("执行失败".mysql_error());  
  45.             $arr=array();  
  46.             $i=0;  
  47.             while ($row=mysql_fetch_assoc($res)){  
  48.                 $arr[$i++]=$row;  
  49.             }  
  50.             //释放资源  
  51.             mysql_free_result($res);  
  52.               
  53.             //获得数据库共有多少行  
  54.             $res=mysql_query($sql2,$this->conn) or die(mysql_errno());  
  55.             if($row=mysql_fetch_row($res)){  
  56.                 $fenyePage->row_Count=$row[0];  
  57.             }  
  58.               
  59.             $fenyePage->res_array=$arr;  
  60.             //共有多少页  
  61.             $fenyePage->pageCount = ceil($fenyePage->row_Count/$fenyePage->pageSize);            
  62.             //释放资源  
  63.             mysql_free_result($res);  
  64.               
  65.             if($fenyePage->pageNow>1){  
  66.                 $prepage = $fenyePage->pageNow - 1;  
  67.                 $navigation_bars="<a href='$php_name?pageNow=$prepage'>上一页</a>";  
  68.             }  
  69.             if($fenyePage->pageNow<$fenyePage->pageCount){  
  70.                 $nextpage = $fenyePage->pageNow + 1;  
  71.                 $navigation_bars .= "<a href='$php_name?pageNow=$nextpage'>下一页</a>";  
  72.             }  
  73.               
  74.             //翻页  
  75.             $start=floor(($fenyePage->pageNow - 1)/$fenyePage->page_num) * $fenyePage->page_num + 1;  
  76.             $index = $start;  
  77.             if($fenyePage->pageNow > $fenyePage->page_num){  
  78.                 $navigation_bars .="<a href='$php_name?pageNow=".($start-1)."'> << </a>";  
  79.             }  
  80.             for(;$start < $fenyePage->pageCount && $start<$index + $fenyePage->page_num;$start++){  
  81.                 $navigation_bars .= "<a href='$php_name?pageNow=$start'>[$start]</a>";  
  82.             }  
  83.             $navigation_bars .="<a href='$php_name?pageNow=".($start+1)."'> << </a>";  
  84.             //显示当前页和共有多少页  
  85.             $navigation_bars .= "  当前页{$fenyePage->pageNow}/共{$fenyePage->pageCount}页";  
  86.             $fenyePage->navigation_bars=$navigation_bars;  
  87.         }  
  88.           
  89.         //执行DML语句 更新 删除 添加  
  90.         public function execute_dml($sql){  
  91.             $b=mysql_query($sql,$this->conn);  
  92.             if(!$b){  
  93.                 return 0;//失败  
  94.             } else {  
  95.                 if(mysql_affected_rows($this->conn)){  
  96.                     return 1;//执行成功  
  97.                 }else{  
  98.                     return 2;//表示没有行发生变化  
  99.                 }  
  100.             }  
  101.         }  
  102.           
  103.           
  104.         //关闭连接的方法  
  105.         public function close_connect(){  
  106.             if($this->conn){  
  107.                 mysql_close($this->conn);  
  108.             }  
  109.         }    
  110.     }  
  111. ?>  
8、fenyepage.class.php
  1. <?php  
  2.     class fenyepage{  
  3.         public $pageSize//每页显示的行数  
  4.         public $pageNow;  //当前页  
  5.         public $pageCount//共有多少页。计算得到  
  6.         public $res_array;//显示数据,数据库获得  
  7.         public $row_Count//共有多少行,数据库获得  
  8.         public $page_num;  //翻页数  
  9.         public $navigation_bars;//导航条  
  10.     }  
  11. ?>  

下面展示的是一个主要的页面:

MVC 的基本概念:

1、  MVC是一种软件的设计模式-》套路

2、  解释下每个字母的含义

M(Model模型:处理业务逻辑 比如 各种类

V (View 视图、界面:PHP编写的

C (Controller 控制器,主要作用是接受用户的请求,并调用某个方法,完成任务,跳转到下一个界面

3、  核心思想

强制程序员在编写项目的时候,把数据的输入、处理、输出分开。

Mvc 的处理过程:

1、  首先控制器接受用户的请求,并决定应该调用那个模型来进行处理

2、  然后调用模型来处理用户的请求并返回数据

3、  最后控制器用相应的视图显示模型返回的数据,并通过浏览器呈现给用户。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值