您的位置:首页 > 编程语言 > PHP开发

使用PHP制作 简易员工管理系统之六(采用分层分页显示用户信息)

2015-06-12 00:09 911 查看
一、目录结构图:



二、文件Admin.class.php管理员类

<?php
class Admin{
private $id;
private $name;
private $password;
/**
* @return $id
*/
public function getId() {
return $this->id;
}

/**
* @return $name
*/
public function getName() {
return $this->name;
}

/**
* @return $password
*/
public function getPassword() {
return $this->password;
}

/**
* @param !CodeTemplates.settercomment.paramtagcontent!
*/
public function setId($id) {
$this->id = $id;
}

/**
* @param !CodeTemplates.settercomment.paramtagcontent!
*/
public function setName($name) {
$this->name = $name;
}

/**
* @param !CodeTemplates.settercomment.paramtagcontent!
*/
public function setPassword($password) {
$this->password = $password;
}

}
?>


三、管理员业务处理类AdminService.class.php(验证信息)

<?php
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 ($row['password']==md5($password)) {
//关闭资源
mysql_free_result($res);
return $row['name'];
}else{
return "";
}
}
//关闭资源
mysql_free_result($res);
//关闭链接
$sqlHelper->close_connect();
return "";

}
}
?>


四、数据库操作工具类SqlHelper.class.php(构造方法数据库链接、查询dql方法、增删改dml方法、分页查询方法)

<?php
require_once 'FenyePage.class.php';
//数据库工具类
class SqlHelper {
public $conn;
public $dbname = "usersDb";
public $username = "root";
public $password = "159357";
public $host = "localhost";

public function __construct(){
$this->conn = mysql_connect($this->host,$this->username,$this->password,$this->conn);
if (!$this->conn) {
die("链接失败!".mysql_errno());
return ;
}
mysql_select_db($this->dbname,$this->conn) or die(mysql_errno());
}

//执行dql语句
public function execute_dql($sql){
mysql_query("set names utf8");
$res = mysql_query($sql,$this->conn);
return $res;
}

//执行dql语句返回数组
public function execute_dql_array($sql){
$arr = array();
$res = mysql_query($sql,$this->conn);
$i = 0;
while ($row = mysql_fetch_assoc($res)){
$arr[$i++]
4000
= $row;
}
//这里可以马上把$res关闭
mysql_free_result($res);
return $arr;
}
//执行dml语句
public function execute_dml($sql){
mysql_query("set names utf8");
$b = mysql_query($sql,$this->conn);
if (!$b) {
return 0;
}else{
if (mysql_affected_rows($this->conn) > 0) {
return 1;//操作成功
}else{
return 2;//没有行数受到影响
}
}
}

//关闭链接
public function close_connect(){
if (empty($this->conn)) {
mysql_close($this->conn);
}
}

//分页查询
//$sql1="select * from 表名 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_errno());
$arr = array();
$i=0;
//将分页数据转移到数组中
while ($row = mysql_fetch_assoc($res)){
$arr[$i++] = $row;
}
//释放资源
mysql_free_result($res);

//将数组赋给分页对象
$fenyePage->setRes_array($arr);//设置分页数据数组

$res1 = mysql_query($sql2,$this->conn) or die(mysql_errno());
if ($row = mysql_fetch_row($res1)) {
$fenyePage->setPageCount(ceil($row[0]/$fenyePage->getPageSize()));//设置分页总数量
}
//释放资源
mysql_free_result($res1);

//封装导航信息
if ($fenyePage->getPageNow()>1) {
$prePage = $fenyePage->getPageNow() -1;
$fenyePage->setDaohang("<a href='erpList.php?pageNow=$prePage'>上一页</a>");
}
if ($fenyePage->getPageNow()< $fenyePage->getPageCount()) {
$nextPage = $fenyePage->getPageNow()+1;
$fenyePage->setDaohang($fenyePage->getDaohang()."<a href='erpList.php?pageNow=$nextPage'>下一页</a>");
}

return $fenyePage;
}
}
?>


五、分页类FenyePage

<?php
class FenyePage{
private $pageSize = 7;
private $res_array;//显示的数据数组
private $rowCount;//从数据库获取的数据条数
private $pageNow;//用户指定当前页
private $pageCount;//数据总条数
private $daohang;//导航信息
/**
* @return $daohang
*/
public function getDaohang() {
return $this->daohang;
}

/**
* @param !CodeTemplates.settercomment.paramtagcontent!
*/
public function setDaohang($daohang) {
$this->daohang = $daohang;
}

/**
* @return $pageSize
*/
public function getPageSize() {
return $this->pageSize;
}

/**
* @return $res_array
*/
public function getRes_array() {
return $this->res_array;
}

/**
* @return $rowCount
*/
public function getRowCount() {
return $this->rowCount;
}

/**
* @return $pageNow
*/
public function getPageNow() {
return $this->pageNow;
}

/**
* @return $pageCount
*/
public function getPageCount() {
return $this->pageCount;
}

/**
* @param !CodeTemplates.settercomment.paramtagcontent!
*/
public function setPageSize($pageSize) {
$this->pageSize = $pageSize;
}

/**
* @param !CodeTemplates.settercomment.paramtagcontent!
*/
public function setRes_array($res_array) {
$this->res_array = $res_array;
}

/**
* @param !CodeTemplates.settercomment.paramtagcontent!
*/
public function setRowCount($rowCount) {
$this->rowCount = $rowCount;
}

/**
* @param !CodeTemplates.settercomment.paramtagcontent!
*/
public function setPageNow($pageNow) {
$this->pageNow = $pageNow;
}

/**
* @param !CodeTemplates.settercomment.paramtagcontent!
*/
public function setPageCount($pageCount) {
$this->pageCount = $pageCount;
}
}
?>


六、用户信息业务处理类ErpService.class.php(分页显示用户信息 、导航条)

<?php
require_once 'SqlHelper.class.php';
//
class ErpService{
//获取分页总数
function getPageCount($pageSize){
$sql = "select count(id) from users";
$sqlHelper = new SqlHelper();
$res = $sqlHelper->execute_dql($sql);
if ($row = mysql_fetch_row($res)) {
$pageCount = ceil($row[0]/$pageSize) ;
}
//释放资源
mysql_free_result($res);
//关闭链接
$sqlHelper->close_connect();
return $pageCount;
}
//获取用户信息
function getUsersListByPage($pageNow,$pageSize){
$sql = "select * from users limit ".($pageNow-1)*$pageSize.",".$pageSize;
$sqlHelper = new SqlHelper();
$arr = $sqlHelper->execute_dql_array($sql);
//关闭链接
$sqlHelper->close_connect();
return $arr;
}

//第二种使用封装的方法完成分页
function getFenyePage($fenyePage){
//创建一个SqlHelper对象实例
$sqlHelper = new SqlHelper();
$sql1 = "select * from users limit ".($fenyePage->getPageNow()-1)*($fenyePage->getPageSize()).",".$fenyePage->getPageSize();
//echo "sql1=$sql1";
//exit();
$sql2 = "select count(id) from users";
//echo "sql2=$sql2";
//exit();
$fenyePage = $sqlHelper->execute_dql_fenye($sql1, $sql2, $fenyePage);
$sqlHelper->close_connect();
return $fenyePage;
}

}
?>


七、员工管理系统登陆界面login.php

<html>
<head>
<meta http-equiv="content-type" content="text/html;cha
fa64
rset=utf-8"/>
<TITLE>管理员登陆系统</TITLE>
<h1>管理员登陆</h1>
<form action="loginProcess.php" method="post">
<table>
<tr><td>用户id</td><td><input type="text" name="id" value="<?php echo $_GET['id'];?>"/></td></tr>
<tr><td>密 码</td><td><input type="password" name="password" value="<?php echo $_GET['password'];?>"/></td></tr>
<tr><td><input type="submit" value="用户登录"/></td>
<td><input type="reset" vallue="重新填写"/></td>
</tr>
</table>
</form>

<?php
//接受错误信息
if (!empty($_GET['error'])) {
$error = $_GET['error'];
if ($error == 1) {
echo "<font color='red' size='3'>您的用户名或者密码错误!</font>";
}
}
?>
</head>
</html>


八、管理员信息验证界面loginProcess.php

<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<?php
require_once 'AdminService.class.php';

//接受用户的数据
//1.id
$id = $_POST['id'];
//2.密码
$password = $_POST['password'];

//实例化AdminService
$adminService = new AdminService();
$name = $adminService->checkAdmin($id, $password);
if ($name!="") {
header("Location: erpManager.php?name=$name");
exit();
}else{
header("Location: login.php?error=1&id=$id&password=$password&name=$name");
exit();
}
?>


九、员工管理系统主界面erpManager.php

<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<?php
require_once 'AdminService.class.php';

//接受用户的数据
//1.id
$id = $_POST['id'];
//2.密码
$password = $_POST['password'];

//实例化AdminService
$adminService = new AdminService();
$name = $adminService->checkAdmin($id, $password);
if ($name!="") {
header("Location: erpManager.php?name=$name");
exit();
}else{
header("Location: login.php?error=1&id=$id&password=$password&name=$name");
exit();
}
?>


十、员工信息显示界面erpList.php

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>显示用户信息</title>
<body>
<?php
require_once 'ErpService.class.php';
$erpService = new ErpService();

//创建一个FenyePage实例
$fenyePage = new FenyePage();
//初始化
$fenyePage->setPageNow(1);//当前页为第一页
$fenyePage->setRowCount(0);//数据库记录条数
$fenyePage->setPageSize(3);//每页显示信息条数

if (!empty($_GET['pageNow'])) {
$fenyePage->setPageNow($_GET["pageNow"]);
}

//业务逻辑进行分页
$fenyePage = $erpService->getFenyePage($fenyePage);

//显示所有用户信息(表格的形式)
echo "<h1>员工信息列表</h1>";
echo "<table border= '1px' width = '700px'>";
echo "<tr><th>id</th><th>name</th><th>grade</th><th>email</th><th>saray</th><th>删除用户</th><th>修改用户</th></tr>";

$arr = $fenyePage->getRes_array();
for ($i=0;$i<count($arr);$i++){
$row = $arr[$i];
echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td><td>{$row['grade']}</td><td>{$row['email']}</td><td>{$row['saray']}</td><td><a href='#'>删除用户</a></td><td><a href='#'>修改用户</a></td></tr>";
}
echo "</table>";

/* if ($fenyePage->getPageNow()>1) {
$prePage = $fenyePage->getPageNow() -1;
echo "<a href='erpList.php?pageNow=$prePage'>上一页</a>";
}
if ($fenyePage->getPageNow()< $fenyePage->getPageCount()) {
$nextPage = $fenyePage->getPageNow()+1;
echo "<a href='erpList.php?pageNow=$nextPage'>下一页</a>";
} */
echo "".$fenyePage->getDaohang();

//设置$start 1----7 floor取整($pageNow-1)/7 = 0 * 7+1 =1  8----15 floor取整($pageNow-1)/7 = 1 *7+1=8
$start = floor(($fenyePage->getPageNow()-1)/7)*7+1;
$index = $start;
if ($fenyePage->getPageNow()>7) {
//向上翻7页
echo "  <a href='erpList.php?pageNow=".($start-1)."'>  <<  </a>";
}

//打印出页码的超链接
for (;$start<$index+7;$start++){
echo "<a href='erpList.php?pageNow=$start'>[$start]</a>";
}
//向下翻7页
echo "<a href='erpList.php?pageNow=$start'>  >></a>";
echo "当前页{$fenyePage->getPageNow()}/共{$fenyePage->getPageCount()}页";
?>
</body>
</head>
</html>


十一、补充文件table.text

//设计数据库
create database usersDb;
管理员表:
admin
create table admin(
id int primary key,
password varchar(64) not null,
name varchar(32) not null
);
用户表
create table users(
id int primary key auto_increment,
name varchar(64) not null,
grade tinyint,/*1表示 1级工*/
email varchar(64) not null,
saray float
);

insert into users(name,grade,email,saray)values('shunping1',1,'xiaohong1@yahoo.com',2000);
insert into users(name,grade,email,saray)values('shunping2',2,'xiaohong2@yahoo.com',3000);
insert into users(name,grade,email,saray)values('shunping3',3,'xiaohong3@yahoo.com',4000);
insert into users(name,grade,email,saray)values('shunping4',4,'xiaohong4@yahoo.com',5000);
insert into users(name,grade,email,saray)values('shunping5',5,'xiaohong5@yahoo.com',6000);
insert into users(name,grade,email,saray)values('shunping6',6,'xiaohong6@yahoo.com',7000);
insert into users(name,grade,email,saray)values('shunping7',7,'xiaohong7@yahoo.com',7000);
insert into users(name,grade,email,saray)values('shunping8',8,'xiaohong8@yahoo.com',7000);
insert into users(name,grade,email,saray)values('shunping9',9,'xiaohong9@yahoo.com',7000);
insert into users(name,grade,email,saray)values('shunping10',10,'xiaohong10@yahoo.com',7000);
insert into users(name,grade,email,saray)values('shunping11',11,'xiaohong11@yahoo.com',7000);
insert into users(name,grade,email,saray)values('shunping12',12,'xiaohong12@yahoo.com',7000);
insert into users(name,grade,email,saray)values('shunping13',13,'xiaohong13@yahoo.com',7000);
insert into users(name,grade,email,saray)values('shunping14',14,'xiaohong14@yahoo.com',7000);
insert into users(name,grade,email,saray)values('shunping15',15,'xiaohong15@yahoo.com',7000);

insert into admin values(1000,md5('admin'),'管理员');

错误处理:
Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\erpManager\loginProcess.php:2) in C:\AppServ\www\erpManager\loginProcess.php on line 31
打开 windows目录找到php.ini 查找到output_buffering = Off改为On即可
然后重新启动 php服务器

//重复插入数据
insert into users(name,grade,email,saray) select name,grade,email,saray from users;


十二、效果图:



项目文件下载地址:http://download.csdn.net/detail/fendouxiaoniao/8797951
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: