07-php雇员管理系统-分层模式实现登录,分页

来源:互联网 发布:怎么给淘宝店做宣传 编辑:程序博客网 时间:2024/06/16 02:03

项目目录结构:


Admin.class.php

<?php//它的一个对象实例就表示admin表中的一条记录class Admin {private $id;private $name;private $password;/** * @return the $id */public function getId() {return $this->id;}/** * @return the $name */public function getName() {return $this->name;}/** * @return the $password */public function getPassword() {return $this->password;}/** * @param $id the $id to set */public function setId($id) {$this->id = $id;}/** * @param $name the $name to set */public function setName($name) {$this->name = $name;}/** * @param $password the $password to set */public function setPassword($password) {$this->password = $password;}}?>


AdminService.class.php

<?php/** * 该类是一个业务逻辑处理类 * 完成对admin表的操作 */require_once 'SqlHelper.class.php';class AdminService {//验证用户是否合法的方法public function checkAdmin($id, $password) {$sql = "select password,name 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'];}else{//""为假return "";}}//资源释放mysql_free_result($res);//关闭连接$sqlHelper->close_connect();return false;}}?>


Emp.class.php

<?php  class Emp{    }?>

empList.php

<html><head><meta content="text/html;charset=utf-8" http-equiv="content-type"><title>雇员信息列表</title></head><?phprequire_once 'EmpService.class.php';$pageSize = 20; //每页数$rowCount = 0; //记录数$pageNow = 1; //当前页//根据用户的点击收取$PageNow的值if (! empty ( $_GET ['pageNow'] )) {$pageNow = $_GET ['pageNow'];}//创建对象实例$empService = new EmpService ();//调用求总共的页数的方法$pageCount = $empService->getPageCount ( $pageSize );//调用getEmplistBypage方法,来获得应当显示的数据$res2 = $empService->getEmpListByPage ( $pageNow, $pageSize );echo "<h1>雇员信息列表</h1>";//表格显示分页查询后的结果echo "<table width='700px' border='1px' bordercolor='green' cellspacing='0px'>";echo "<tr><th>id</th><th>name</th><th>grade</th>";echo "<th>email</th><th>salary</th><th>删除</th><th>修改</th></tr>";for($i = 0; $i < count ( $res2 ); $i ++) {$row = $res2 [$i];echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td><td>{$row['grade']}</td>";echo "<td>{$row['email']}</td><td>{$row['salary']}</td>";echo "<td><a href='#'>删除用户</a></td>";echo "<td><a href='#'>修改用户</a></td><tr>";}echo "</table>";//显示上一页和下一页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}页";echo "<br/><br/>";?> <form action="empList.php" method="get">跳转到:<input type="text"name="pageNow" style="width: 40px;" />页 <input type="submit" value="Go" /></form></html>


empManage.php

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

EmpService.class.php

<?phprequire_once 'SqlHelper.class.php';class EmpService {//一个函数,可以用来获取公有多少页function getPageCount($pageSize) {//需要查询到$rowCount$sql = "select count(id) from emp";$sqlHelper = new SqlHelper ();$res = $sqlHelper->execute_dql ( $sql );//这样就可以计算pageCountif ($row = mysql_fetch_row ( $res )) {$pageCount = ceil ( $row [0] / $pageSize );}//释放资源,关闭连接mysql_free_result ( $res );$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;}}?>

login.php

<html><head><meta content="text/html;charset=utf-8" http-equiv="content-type"></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="password" name="password" /></td></tr><tr><td><input type="submit" value="用户登录" /></td><td><input type="reset" value="重新填写" /></td></tr></table></form><?phpif (! empty ( $_GET ['errno'] )) {$errno = $_GET ['errno'];if ($errno == 1) {echo "<font color='red' size='3'>你的用户名或密码错误</font>";}}?></html>

loginProcess.php

<?phprequire_once 'AdminService.class.php';//接受用户的数据//1.id$id = $_POST ['id'];//2.密码$password = $_POST ['password'];//实例化一个AdminService方法$adminService = new AdminService ();if ($adminService->checkAdmin ( $id, $password )) {$name=$adminService->checkAdmin ( $id, $password );//合法header ( "Location:empManage.php?name=$name" );exit ();} else {//非法header ( "Location:login.php?errno=1" );exit ();}?>

SqlHelper.class.php

<?php/** * 操作数据库的工具类 * 作用是完成对数据库的操作 * @author Administrator * */class SqlHelper {public $conn; //数据库连接public $dbname = "test"; //数据库的名称public $username = "root"; //public $password = "root";public $host = "localhost";//构造函数public function __construct() {$this->conn = mysql_connect ( $this->host, $this->username, $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;}//执行dql语句,但是返回的是一个数组public function execute_dql2($sql) {$arr = array ();$res = mysql_query ( $sql, $this->conn ) or die ( mysql_error () );$i = 0;//把$res中的资源放到数组中区while ( $row = mysql_fetch_assoc ( $res ) ) {$arr [$i ++] = $row;}//这里可以立即关闭$resmysql_free_result ( $res );return $arr;}//执行dml语句public function execute_dml($sql) {$b = mysql_query ( $sql, $this->conn );if (! $b) {return 0; //表示失败} else {if (mysql_affected_rows ( $this->conn ) > 0) {return 1; //表示执行ok} else {return 2; //表示没有受影响的行}}}//关闭连接的方法public function close_connect() {if (!empty ( $this->conn )) {mysql_close ( $this->conn );}}}?>

运行效果:





原创粉丝点击