这个章节将讲解分层模式对雇员管理系统的系统,首先看下基本的流程图设计:
下面是具体的代码:
<?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(); } ?>
<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;//导航条 } ?>
下面展示的是一个主要的页面: