MVC模式学习--雇员管理系统项目开发

1, 项目开发,使用原型开发,

① 开发流程:

需求分析->设计阶段->编码阶段->测试阶段->发布阶段/维护阶段

需求阶段:PM/项目经理 对客户

设计阶段:技术人员(架构师,组长,程序员)->设计数据库

开发阶段:组长,程序员

测试阶段:测试人员

发布阶段:实施工程师

维护阶段:开发人员

② 对应于php研发流程:

需求阶段:PM与项目经理定好需求,美工UI(ps画图)->网页前端人员UE(div+css),PM和项目经理根据静态页面与客户探讨不断修改;

设计阶段:架构师设计框架,设计数据库;

开发阶段: php研发人员与js前端设计人员开发;

测试阶段:测试人员测试;

发布阶段:php研发人员与js前端设计人员发布;

维护阶段:php研发人员与js前端设计人员维护;

2, 需求设计:

① 管理员可以登录到管理界面;

② 管理员可以对雇员进行增加;

③ 管理员可以对雇员进行修改;

④ 管理员可以查看雇员(分页显示);

⑤ 管理员可以删除雇员;

附带:可以实现用户在一定时间内不用反复登陆;可以统计网站被访问的次数。

3, UML时序图

MVC模式学习--雇员管理系统项目开发

4, 美工UI图片,UE设计静态页面,界面设计

登录界面,主界面,显示记录页面

5, 设计数据库,管理员数据表和用户数据表(使用PowerDesigner)

CDM(Categories---Information---Conceptual Data)(TOOLS—Model option –Notation----E/R+Merise)

MVC模式学习--雇员管理系统项目开发

PDM(TOOLS---Generate Physical Data Model 选择mysql5.0)

MVC模式学习--雇员管理系统项目开发

生成sql语句(Database---Generate Database)。生成内容如下:

/*==============================================================*/
/* DBMS name: MySQL 5.0 */
/* Created on: 2015/11/4 1:03:19 */
/*==============================================================*/ drop table if exists admin; drop table if exists emp; /*==============================================================*/
/* Table: admin */
/*==============================================================*/
create table admin
(
id int not null auto_increment,
name varchar(64),
password varchar(64),
primary key (id)
); /*==============================================================*/
/* Table: emp */
/*==============================================================*/
create table emp
(
id int not null auto_increment,
name varchar(64),
grade int,
email varchar(64),
salary float,
primary key (id)
);

注:将emp数据表的id1修改为id,执行以下语句,生成数据表。

6, 代码阶段

① Model1模式,没有分层概念,也就是把界面显示和业务逻辑的处理放在一个文件夹中完成。

② 分层模式,界面和业务逻辑分离。

③ MVC模式,在分层模式中加控制器,强制把数据的输入,数据的处理,数据的显示分开。PHP中MVC框架Zend framework,thinkphp,cakephp,yii。

7, MVC设计模式,MVC是一种软件设计模式,分为M、V、C三部分。

① M(model模型),处理业务逻辑的类文件,比如Admin.class.php,AdminServer.class.php以及工具类等;

② C(controller控制器),控制器的主要作用是接收用户的请求,并调用某个service方法,完成任务,然后跳转到下一个页面;

③ V(view视图/界面),前端页面;

MVC的核心思想就是强制程序员在编写项目时候,把数据的输入/数据的处理/数据的输出分开。具体流程如下:

MVC模式学习--雇员管理系统项目开发

没有必要对每一个请求,对应生成一个控制器,可以把同一逻辑的请求,提交给一个控制器(数据表)即可。

8, 代码层次如下,通过下图可知,项目没有完全MVC模块化,需要以后继续优化

MVC模式学习--雇员管理系统项目开发

代码页目录结构如下:

MVC模式学习--雇员管理系统项目开发

具体每个代码页:

login.php

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=GB2312" />
<title>雇员管理系统登陆页面</title>
</head>
<body>
<form action="loginProcess.php" method = "post">
用户名:<input type="text" name="username" /><br/>
密码:<input type="password" name="passwd" /><br/>
<input type="submit" value="登陆" name="sub" /><input type="reset" value="重新填写" name="res" />
</form>
</body>
</html>

loginProcess.php

<?php
require_once "AdminServer.class.php";
$username = $_POST['username'];
$passwd = $_POST['passwd'];
if(!$username || !$passwd){
echo "用户名或密码不能为空!,请重新<a href='login.php'>登陆</a>";
} if(AdminServer::checkLogin($username, $passwd)){
header("Location:empManage.php?name={$username}");
}else{
header("Location:login.php");
}

empManage.php

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=GB2312" />
<title>用户管理页面</title>
</head>
<body>
<?php
echo $_GET['name'];
?>,欢迎你
<h1>主界面</h1>
<a href="empList.php">管理用户</a><br/>
<a href="empAdd.php">添加用户</a><br/>
<a href="#">查询用户</a><br/>
<a href="login.php">重新登录</a><br/>
<a href="#">退出系统</a><br/>
</body>
</html>

empList.php

<?php
require_once "fenyePage.class.php"; //显示第几页内容
$pageNow = $_GET['pageNow']?$_GET['pageNow']:1;
$fenyePage = new fenyePage('emp', $pageNow, 'empList.php'); $res = $fenyePage->getListByFenye(); echo "<table border=1>";
echo "<tr><th>用户ID</th><th>用户名</th><th>邮件</th><th>级别</th><th>修改用户</th><th>删除用户</th></tr>";
foreach($res as $key => $row){
echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td><td>{$row['email']}</td><td>{$row['grade']}</td><td><a href='empUpdate.php?id={$row['id']}'>修改用户</a></td><td><a href='empProcess.php?flag=del&id={$row['id']}'>删除用户</a></td></tr>";
}
echo "</table>"; $fenyeBanner = $fenyePage->getFenyeBanner();
echo $fenyeBanner;
?>

empProcess.php

<?php
require_once "EmpServer.class.php"; $empServer = new EmpServer();
$flag = $_REQUEST['flag'];
if(!empty($flag)){
if($flag == 'del'){ // 执行删除操作
$id = $_GET['id'];
$b = $empServer->delEmpById($id);
if($b == 1){
// echo "删除成功!";
header("Location:empList.php");
}else{
echo "删除失败!";
}
}else if($flag == 'add'){ //执行添加操作
$name = $_POST['name'];
$grade = $_POST['grade'];
$email = $_POST['email'];
$salary = $_POST['salary'];
$b = $empServer->addEmp($name, $grade, $email, $salary); // $emp = new Emp();
// $emp->setName($name);
// $emp->setGrade($grade);
// $emp->setEmail($email);
// $emp->setSalary($salary); // $b = $empServer->addEmp($emp);
}else if($flag == 'update'){ //执行修改操作
$id = $_POST['id'];
$name = $_POST['name'];
$grade = $_POST['grade'];
$email = $_POST['email'];
$salary = $_POST['salary']; $b = $empServer->updateEmpById($id, $name, $grade, $email, $salary);
if($b){
header("Location:empList.php");
}else{
echo "修改失败!";
}
} }

empAdd.php

<h1>添加用户</h1>
<form action="empProcess.php" method="post">
<input type="hidden" name="flag" value="add">
<table>
<tr><td>用户名:</td><td><input type="text" name="name" /></td></tr>
<tr><td>级别:</td><td><input type="text" name="grade" /></td></tr>
<tr><td>邮箱:</td><td><input type="text" name="email" /></td></tr>
<tr><td>工资:</td><td><input type="text" name="salary" /></td></tr>
<tr><td colspan="2"><input type="submit" value="添加用户" /><input type="reset" value="重新填写"></td></tr>
</table>
</form>

empUpdate.php

<?php
$id = $_GET['id'];
require_once "EmpServer.class.php";
$empServer = new EmpServer();
$res = $empServer->getEmpById($id);
$row = $res[0];
?>
<h1>修改用户</h1>
<form action="empProcess.php" method="post">
<input type="hidden" name="flag" value="update">
<input type="hidden" name="id" value="<?=$id?>">
<table>
<tr><td>用户名:</td><td><input type="text" name="name" value="<?=$row['name']?>" /></td></tr>
<tr><td>级别:</td><td><input type="text" name="grade" value="<?=$row['grade']?>" /></td></tr>
<tr><td>邮箱:</td><td><input type="text" name="email" value="<?=$row['email']?>" /></td></tr>
<tr><td>工资:</td><td><input type="text" name="salary" value="<?=$row['salary']?>" /></td></tr>
<tr><td colspan="2"><input type="submit" value="修改用户" /><input type="reset" value="重新填写"></td></tr>
</table>
</form>

Admin.class.php

<?php
class Admin
{
private $id;
private $name;
private $password; public function setId($id){
$this->id = $id;
} public function getId(){
return $this->id;
} public function setName($name){
$this->name = $name;
} public function getName(){
return $this->name;
} public function setPassword($password){
$this->password = $password;
} public function getPassword(){
return $this->password;
} }

AdminServer.class.php

<?php
require "SqlHelper.class.php";
class AdminServer
{ //验证登陆正确
public function checkLogin($username, $password){
$b = false;
$sqlHelper = new SqlHelper();
$sql = "select password from admin where name = '{$username}'";
$res = $sqlHelper->execute_query($sql);
if($res){
while($row = mysqli_fetch_assoc($res)){
if($row['password'] == md5($password)){
$b = true;
}
}
}
$sqlHelper->close_connect();
return $b;
} }

Emp.class.php

<?php
class Emp
{
private $id;
private $name;
private $grade;
private $email;
private $salary; public function setId($id){
$this->id = $id;
} public function getId(){
return $this->id;
} public function setName($name){
$this->name = $name;
} public function getName(){
return $this->name;
} public function setGrade($grade){
$this->grade = $grade;
} public function getGrade(){
return $this->grade;
} public function setEmail($email){
$this->email = $email;
} public function getEmail(){
return $this->email;
} public function setSalary($salary){
$this->salary = $salary;
} public function getSalary(){
return $this->salary;
}
}

EmpServer.class.php

<?php
require_once "SqlHelper.class.php";
class EmpServer
{
//根据id删除某条记录
public function delEmpById($id){
$sqlHelper = new SqlHelper();
$sql = "delete from emp where id = {$id}";
$b = $sqlHelper->execute_dml($sql);
$sqlHelper->close_connect();
return $b;
} //添加某条记录
public function addEmp($name, $grade, $email, $salary){
$sqlHelper = new SqlHelper();
$sql = "insert into emp(name,grade,email,salary) VALUES ('{$name}','{$grade}','{$email}','{$salary}')";
$res = $sqlHelper->execute_dml($sql);
$sqlHelper->close_connect();
return $res;
} //查询某个id对应的记录
public function getEmpById($id){
$sqlHelper = new SqlHelper();
$sql = "select * from emp where id = {$id}";
$res = $sqlHelper->execute_dql($sql);
$sqlHelper->close_connect();
return $res;
} //修改某个id对应的记录
public function updateEmpById($id, $name, $grade, $email, $salary){
$sqlHelper = new SqlHelper();
$sql = "update emp set name='{$name}',grade='{$grade}',email='{$email}',salary='{$salary}' where id = '{$id}'";
$res = $sqlHelper->execute_dml($sql);
$sqlHelper->close_connect();
return $res;
}
}

fenyePage.class.php

<?php
require_once "SqlHelper.class.php";
class fenyePage
{
private $table;
private $pageNow;
private $pageCount;
private $rowCount;
private $pagesize;
private $gotoUrl; public function __construct($table, $pageNow, $gotoUrl){
$sqlHelper = new SqlHelper();
$this->table = $table;
$this->pageNow = $pageNow;
$this->pageSize = 2;
$getCountSql = "select count(id) as num from {$table}";
$this->rowCount = $sqlHelper->getCount($getCountSql);
if($this->pageSize){
$this->pageCount = ceil($this->rowCount/$this->pageSize);
}
$this->gotoUrl = $gotoUrl;
} //获取某一页的记录数据
public function getListByFenye(){
$sqlHelper = new SqlHelper();
//显示某页记录数
$limitBegin = $this->pageSize*($this->pageNow - 1); $getListPerPageSql = "select * from {$this->table} limit $limitBegin,$this->pageSize";
$res = $sqlHelper->execute_dql($getListPerPageSql);
return $res;
} //获取分页下部分
public function getFenyeBanner(){
$pageNext = ((int)$this->pageNow+1) > $this->pageCount?$this->pageCount:((int)$this->pageNow+1);
$pagePeriod = ((int)$this->pageNow-1) < 1?1:((int)$this->pageNow-1);
return "<form action='{$this->gotoUrl}'><a href='{$this->gotoUrl}?pageNow=1'>首页</a>  
<a href='{$this->gotoUrl}?pageNow={$pagePeriod}'>上一页</a>  
<a href='{$this->gotoUrl}?pageNow={$pageNext}'>下一页</a>  
<a href='{$this->gotoUrl}?pageNow={$this->pageCount}'>尾页</a>  
<input type='text' name='pageNow' value='{$this->pageNow}'>/{$this->pageCount}<input type='submit' value='GO'>
</form>";
} }

SqlHelper.class.php

<?php
//操作数据库的工具类
class SqlHelper
{
private $conn;
private $host = "10.252.158.217"; //118.207.76.55
private $username="root";
private $password="521lhy";
private $dbname = "test"; //类的初始化,获得链接
public function __construct(){
$this->conn = new MySQLi($this->host, $this->username, $this->password, $this->dbname);
if($this->conn->connect_error){
die("连接错误!".$this->conn->connect_error);
} $this->conn->query("set names utf-8");
} //执行sql语句
public function execute_query($sql){
$res = $this->conn->query($sql) or die($this->conn->error);
return $res;
} //执行dql语句
public function execute_dql($sql){
$arr = array();
$res = $this->conn->query($sql) or die($this->conn->error);
while($row = mysqli_fetch_assoc($res)){
$arr[] = $row;
}
mysqli_free_result($res);
return $arr;
} //获取某数据表记录总数
public function getCount($sql){
$rowCount = 0;
$res = $this->execute_query($sql);
if($row = mysqli_fetch_assoc($res)){
$rowCount = $row['num'];
}
mysqli_free_result($res);
return $rowCount;
} //执行sql语句
public function execute_dml($sql){
$res = $this->conn->query($sql) or die($this->conn->error);
if(!$res){
return 0;
}else{
if(mysqli_affected_rows($this->conn) > 0){
return 1;
}else{
return 2;
}
}
} //关闭连接
public function close_connect(){
if($this->conn){
$this->conn->close();
}
}
}
上一篇:iOS UI进阶-5.0 蓝牙/加速计/传感器


下一篇:JS 在火狐浏览器下关闭弹窗