您的位置:首页 > 运维架构

分层模式:OOP分页管理系统

2015-07-29 14:31 696 查看
                                          工具类:SqlHelper.class.php

<?php
class SqlHelper{
public $mysqli;
public $host="localhost";
public $username="root";
public $password="123456";
public $dbname="emp";
//自动创建MYSQL连接;
public function __construct(){
$this->mysqli=new mysqli($this->host,$this->username,$this->password,$this->dbname);
if($this->mysqli->connect_error){
die ("LINK FAILED".$this->mysqli->connect_error);
}
$this->mysqli->query("set names utf8");
}
//实现查询功能;
public function exe_dql($query){
$res=$this->mysqli->query($query) or die($this->mysqli->error);
return $res;
}
//用数组方式获取资源,及时释放结果集;
public function exe_dql2($query){

$arr=array();
$res=$this->mysqli->query($query) or die($this->mysqli->error);
while($row=$res->fetch_assoc()){
$arr[]=$row;
}
$res->free();
//返回一个二维数组;
return $arr;
}
//实现分页功能;因为$paging是个对象,可以不加引用符号;
//$query1是查询语句,$query2是计算记录条数语句;
public function exe_paging($query1,$query2,$paging){
$res1=$this->mysqli->query($query1) or die($this->mysqli->error);
$arr=array();
while($row=$res1->fetch_assoc()){
$arr[]=$row;
}
$res1->free();
$res2=$this->mysqli->query($query2) or die($this->mysqli->error);
if($row=$res2->fetch_row()){
$paging->rowCount=$row[0];
//用ceil函数获取总页数;
$paging->pageCount=ceil($row[0]/$paging->pageSize);
}
//将数组结果赋给对象;
$paging->res_arr=$arr;
//导航条;高度封装,实现页面显示与业务逻辑高度分离;
$navigator="";
if($paging->pageNow>1){
$prePage=$paging->pageNow-1;
$navigator= "<a href='empList.php?pageNow=$prePage'>上一页</a> ";
}
if($paging->pageNow<$paging->pageCount){
$nextPage=$paging->pageNow+1;
$navigator.= "<a href='empList.php?pageNow=$nextPage'>下一页</a> ";
}
$paging->navigator=$navigator;
}
//操作语句,返回状态码;
public function exe_dml($query){
$res = $this->mysqli->query($query) or die($this->mysqli->error);
if (!$res) {
return 0;
} else {
if ($this->mysqli->affected_rows > 0) {
return 1;
} else {
return 2;
}
}
}

//关闭数据库连接;
public function close_link(){
if(!empty($this->mysqli)){
$this->mysqli->close();
}
}

}

                                    分页类:Paging.class.php

<?php
class Paging{
public $pageSize=8;//这个可以自由指定,暂时初始化一个数;
public $pageNow=1;//先默认为第一页;
public $pageCount;//可由工具类获取;
public $rowCount;//可由工具类获取;
public $res_arr;//可由工具类获取;
public $navigator;//可由工具类获取;

}

                                    表服务类:EmpService.class.php

<?php
require_once 'SqlHelper.class.php';
class EmpService{

function getPaging($paging){
//分页算法;
$query1= "select * from emp limit ".($paging->pageNow-1)*$paging->pageSize.",
$paging->pageSize";
$query2= "select count(id) from emp";
$sqlHelper=new SqlHelper();
$sqlHelper->exe_paging($query1,$query2,$paging);

}

}

                                         页面显示:EmpList.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>雇员信息列表</title>
</head>
<?php
require_once './LayOut/EmpService.class.php';
require_once './LayOut/Paging.class.php';
$paging=new Paging();
$paging->pageNow=1;
$paging->pageSize=20;

//通过GET方法,指定要跳转到的页数;
if(!empty($_GET['pageNow'])){
$paging->pageNow=$_GET['pageNow'];
}
//创建表服务对象,执行分页函数;
$empService=new EmpService();
$empService->getPaging($paging);

echo "<table width='700px' border='1' bordercolor='yellow' cellspacing='0px'>";
echo "<tr><th>id</th><th>name</th><th>grade</th><th>email</th><th>salary</th>
<th>删除用户</th><th>修改用户</th></tr>";
for($i=0;$i<count($paging->res_arr);$i++){
$row=$paging->res_arr[$i];
echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td><td>{$row['grade']}</td>
<td>{$row['email']}</td><td>{$row['salary']}</td><td>
<a href='?'>删除用户</a></td><td><a href='?'>修改用户</a> </td></tr>";
}
echo "<h1>雇员信息列表</h1>";
echo "</table>";
//直接输出导航条;
echo $paging->navigator;
//实现整体翻页功能;
$page_whole=10;
$start=floor(($paging->pageNow-1)/$page_whole)*$page_whole+1;
$index=$start;
if($paging->pageNow>$page_whole){
echo "<a href='empList.php?pageNow=".($start-1)."'>  <<</a>  ";
}
if($paging->pageNow<$paging->pageCount) {
for (; ($start < $index + $page_whole)&&($start<=$paging->pageCount); $start++) {
echo "<a href='empList.php?pageNow=$start'>[$start]</a> ";
}
echo "<a href='empList.php?pageNow=$start'>  >> </a>";
}
echo "当前页{$paging->pageNow}/总共{$paging->pageCount}页";

?>
<form action="empList.php">
<input type="text" name="pageNow">
<input type="submit" value="GO">
</form>

<body>

</body>
</html>










内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  分页 PHP