08-php雇员管理系统-分层模式实现通用分页

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>

<?php
require_once 'EmpService.class.php';
require_once 'FenyePage.php';

//创建一个fenyePage对象实例
$fenyePage = new FenyePage ();
$fenyePage->pageNow = 1;
$fenyePage->pageSize = 6;

//根据用户的点击收取$PageNow的值
if (! empty ( $_GET ['pageNow'] )) {
	$fenyePage->pageNow = $_GET ['pageNow'];
}
//创建对象实例
$empService = new EmpService ();
$empService->getFenyePage ( $fenyePage );

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 ( $fenyePage->res_array ); $i ++) {
   $row=$fenyePage->res_array[$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>";

//显示上一页和下一页
echo $fenyePage->navigate;

/*
//使用for循环打印超链接
$page_whole = 5;
$start = floor ( ($fenyePage->pageNow - 1) / $page_whole ) * $page_whole + 1;
$index = $start;

//整体向前翻10页
//如果当前pageNow在1-10,没有向前翻动的超链接
if ($pageNow > $page_whole) {
	echo "  <a href='empList.php?pageNow=" . ($start - 1) . "'>  <<  </a>";
}
for(; $start < $index + $page_whole; $start ++) {
	echo "<a href='empList.php?pageNow=$start'>[$start]</a>";
}
//整体向后10页翻动
echo "  <a href='empList.php?pageNow=$start'>  >>  </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>
<?php
echo "欢迎你," . $_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

<?php
require_once 'SqlHelper.class.php';
class EmpService {
	//一个函数,可以用来获取公有多少页
	function getPageCount($pageSize) {
		//需要查询到$rowCount
		$sql = "select count(id) from emp";
		$sqlHelper = new SqlHelper ();
		$res = $sqlHelper->execute_dql ( $sql );
		//这样就可以计算pageCount
		if ($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;
	}
	//第二种使用封装的方式完成分页
	function getFenyePage($fenyePage) {
		//创建一个SqLHelper对象实例
		$sqlHelper = new SqlHelper ();
		$sql1 = "select * from emp  limit " . ($fenyePage->pageNow - 1) * $fenyePage->pageSize . "," . $fenyePage->pageSize;
		$sql2 = "select count(id) from emp";
		$sqlHelper->execute_dql_fenye ( $sql1, $sql2, $fenyePage );
		$sqlHelper->close_connect();
	}
}
?>

FenyePage.php

<?php
//这是一个用于分页信息的类
class FenyePage{
	public $pageSize=6;
	public  $res_array;//这是显示数据
	public $rowCount;//这是从数据库中获取
	public $pageNow;//用户指定的
	public $pageCount;//这个是计算得到的总页数
	public $navigate;//分页导航
}

?>

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>
<?php
if (! empty ( $_GET ['errno'] )) {
	$errno = $_GET ['errno'];
	if ($errno == 1) {
		echo "<font color='red' size='3'>你的用户名或密码错误</font>";
	}
}
?>
</html>

loginProcess.php

<?php
require_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 () );
		//把$res中的资源放到数组中区
		while ( $row = mysql_fetch_assoc ( $res ) ) {
			$arr [] = $row;
		}
		//这里可以立即关闭$res
		mysql_free_result ( $res );
		return $arr;
	}
	
	//考虑分页情况的查询
	//sql1="select * from where limit 0,6";
	//sql2="select count(id) from ";
	public function execute_dql_fenye($sql1, $sql2, $fenyePage) {
		//这里查询到了要分页显示的数据
		$res = mysql_query ( $sql1, $this->conn ) or die ( mysql_error () );
		$arr = array ();
		//把$res转移到$arr
		while ( $row = mysql_fetch_assoc ( $res ) ) {
			$arr [] = $row;
		}
		mysql_free_result ( $res );
		
		$res2 = mysql_query ( $sql2, $this->conn ) or die ( mysql_error () );
		if ($row = mysql_fetch_row ( $res2 )) {
			$fenyePage->pageCount = ceil ( $row [0] / $fenyePage->pageSize );
			$fenyePage->rowCount = $row [0];
		}
		mysql_free_result ( $res2 );
		
		//把导航信息也封装到fenyePage对象中
		$navigate="";
		if ($fenyePage->pageNow > 1) {
			$prePage = $fenyePage->pageNow - 1;
			$navigate= "<a href='empList.php? pageNow=$prePage'>上一页</a> ";
		}
		if ($fenyePage->pageNow < $fenyePage->pageCount) {
			$nextPage = $fenyePage->pageNow + 1;
			$navigate.= "<a href='empList.php? pageNow=$nextPage'>下一页</a> ";
		}
		//把$arr赋值给$fenyePage
		$fenyePage->res_array = $arr;
		$fenyePage->navigate=$navigate;
	}
	
	//执行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 );
		}
	}
}
?>



项目目录结构:




运行效果:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会编程的阿强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值