前面已经介绍了Zend studio工具的使用以及软件开发的基本阶段,下面就是我们第一个练习,雇员管理系统,从设计到实现来简单介绍下:
开发环境:
服务器:基于Linux 2.618环境下配置PHP服务器
Ide : Zend studio
下面是基本的框架图:
1、首先设计数据库
- 设计数据库
- Admin
- Create table admin(
- Id int primary key,
- Name varchar(32) not null,
- Password varchar(128) not null
- );
- Emp 雇员表
- Create table emp(
- Id int primary key auto_increment,
- Name varchar(64) not null,
- Grade tinyint,
- Email varchar(64) not null,
- Salary float
- )
- 添加用户
- insert into admin(id,name,password) value('124','admin',md5('admin'));
- insert into emp(name,grade,email,salary) value('admin',’80’,’3789131@163.com’,10);
2、代码实现
- <!-- login.php -->
- <html>
- <head>
- <meta http-equiv="content-tpe" content="text/html;charset-utf-8"/>
- </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="text" name="password"/></td></tr>
- <tr><td><input type="submit" value="用户登录"/></td>
- <td><input type="reset" value="重新填写"/></td></tr>
- </table>
- </form>
- <?php
- if(!empty($_GET['errno'])){
- $errno=$_GET['errno'];
- if($errno==1){
- echo "</br><font color='black' size='3px'>你的用户名或者密码输入错误 </font> ";
- }else{
- echo "hello";
- }
- }
- ?>
- </html>
- <!-- loginProcess.php -->
- <?php
- /**
- *
- * @author jsh
- * @version
- */
- //接受用户数据
- $id=$_POST['id'];
- $password=$_POST['password'];
- //到数据库进行验证
- //链接数据库
- $mysql_con=mysql_connect("192.168.1.110:3306","root","");
- if(!$mysql_con){
- die("connect error");
- }
- //选择数据库
- mysql_select_db("manage",$mysql_con);
- //验证
- $sql="select * from admin where id=$id";
- $res=mysql_query($sql,$mysql_con);
- if(($row = mysql_fetch_assoc($res)) != ""){
- if(md5($password) == $row['Password']){
- $name = $row['Name'];
- 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();
- }
- } else {
- header("Location:https://192.168.1.110/myphp/manage/login.php?errno=1");
- // //如果登陆不成 功,需要提示对话框显示用户名或者密码错误
- exit();
- }
- mysql_free_result($res);
- // //简单验证
- // if($id == "jsh" && $password == "1234567a"){
- // header("Location:https://192.168.1.110/myphp/manage/empManage.php");
- // exit();
- // }else{
- // //非法用户,跳转到登陆页面
- // header("Location:https://192.168.1.110/myphp/manage/login.php?errno=1");
- // //如果登陆不成 功,需要提示对话框显示用户名或者密码错误
- // exit();
- // }
- ?>
- <!-- empManage.php -->
- <html>
- <head>
- <meta http-equiv="content-tpe" content="text/html;charset-utf-8"/>
- </head>
- <?php
- $name=$_GET['name'];
- echo "欢迎".$name."进入";
- echo " <a href='login.php'>返回登陆界面</a></br>"
- ?>
- <h1>主界面</h1>
- <a href='empList.php'>管理用户</a></br>
- <a href=''>添加用户</a></br>
- <a href=''>查询用户</a></br>
- <a href=''>退出系统</a></br>
- </html>
下面是关于分页技术的介绍:
在分页中有几个变量是必须的:
$pageNow :显示第几页:用户输入
$pageCount:共有几页[]
$rowCount:共有多少条记录[数据库获取]
$pagesize:每页显示几条记录[人为定义]
计算pageCount 算法
<?php
$pageCount=ceil($rowCount/$pagesize);
?>
显示从第几个数据开始显示多少数据的SQL语句(起始是 0)
- select * from emp limit 1,3;
- Select count(Id) from emp;
- <!-- emplist.php -->
- <html>
- <head>
- <meta http-equiv="content-tpe" content="text/html;charset-utf-8"/>
- <title>雇员管理列表</title>
- </head>
- <?php
- $mysql_con=mysql_connect("192.168.1.110:3306","root","");
- if(!$mysql_con){
- die("connect error");
- }
- //选择数据库
- mysql_select_db("manage",$mysql_con);
- /*
- $pageNow :显示第几页:用户输入
- $pageCount:共有几页[]
- $rowCount:共有多少条记录[数据库获取]
- $pagesize:每页显示几条记录[人为定义]
- */
- $pagesize = 3;
- //获取共有多少记录
- $sql="select count(Id) from emp";
- $res=mysql_query($sql,$mysql_con);
- if($row=mysql_fetch_array($res)){
- $rowCount=$row[0];
- }
- mysql_free_result($res);
- $pageCount = ceil($rowCount/$pagesize);
- if(!empty($_GET['pageNow'])){
- $pageNow = $_GET['pageNow'];
- } else {
- $pageNow = 1;
- }
- //验证'
- $sql="select * from emp limit ".($pageNow-1)*$pagesize.",$pagesize";
- $res=mysql_query($sql,$mysql_con);
- 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>";
- while(($row=mysql_fetch_assoc($res)) != ""){
- echo "<tr><th>{$row['Id']}</th><th>{$row['Name']}</th><th>{$row['Grade']}</th>".
- "<th>{$row['Email']}</th><th>{$row['Salary']}</th><th><a href=''>删除用户</a></th>".
- "<th><a href=''>修改用户</a></th></tr>";
- }
- echo "</table>";
- /*
- //打印出页码的链接
- for($i=1;$i<=$pageCount;$i++){
- echo "<a href='empList.php?pageNow=$i'><$i></a>";
- }
- */
- //显示上一页和下一页
- 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}页";
- //指定跳转到某页
- ?>
- <form action="empList.php" method="get">
- 跳转到:<input type="text" name="pageNow"/>
- <input type="submit" value="GO"/>
- </form>
- <?php
- mysql_free_result($res);
- ?>
- </html>
这里我们必须提到,在软件开发中有几个重要的开发模式
1、Model1 模式
2、分层模式(界面、业务逻辑分离)
3、Mvc 模式
Model1模式最重要的特点就是没有分层的概念,也就是说界面显示和业务逻辑的处理放在一个文件中来完成。
缺点:
1)、表现层和业务逻辑层混合在一起(乱!)
2)、在开发中,不利于多人的协同开发
3)、不利于后期的维护
优点:
简单、开发速度比较快
比较适合开发小的项目
分层模式 基本概念是:把界面和业务分层-〉结构清晰,利于分工开发
Mvc模式,基本概念:强制把数据的输入,数据的处理,数据的显示
下面章节将介绍 分层模式及Mvc模式实现!
参考资料:
传智播客视频