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

                                          工具类: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>&nbsp;";
        }
        if($paging->pageNow<$paging->pageCount){
            $nextPage=$paging->pageNow+1;
            $navigator.= "<a href='empList.php?pageNow=$nextPage'>下一页</a>&nbsp;";
        }
        $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)."'>&nbsp;&nbsp<<</a>&nbsp;&nbsp;";
}
if($paging->pageNow<$paging->pageCount) {
    for (; ($start < $index + $page_whole)&&($start<=$paging->pageCount); $start++) {
        echo "<a href='empList.php?pageNow=$start'>[$start]</a>&nbsp;";
    }
    echo "<a href='empList.php?pageNow=$start'>&nbsp;&nbsp;>>&nbsp;</a>";
}
echo "当前页{$paging->pageNow}/总共{$paging->pageCount}";


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

<body>

</body>
</html>







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值