角色权限管理系统(工具类部分)

来源:互联网 发布:新卷皮 淘宝客 源码 编辑:程序博客网 时间:2024/04/25 09:14

为了更好的开发,这次项目我写了几个比较有用的工具类主要就围绕数据库操作、分页、session来弄的一起来看看吧

1.数据库连接工具类

<?php   class DBUtil{   private $host="localhost";   private $username="root";   private $password="123456";   private $dbname="humanresource";   private $conn;   public function DBUtil(){        $this->conn=new mysqli($this->host, $this->username, $this->password,$this->dbname) or die($this->conn->connect_error);       }     public function getConnection(){     return $this->conn;   }     public function close(){   if($this->conn){   $this->conn->close();   }        }    }?>

这个类主要用于取得或关闭数据库连接


2.数据库操作类

<?phpclass BaseDBOperate {   public function query($sql,$conn){   //设置查询编码,我本地的数据库编码为UTF-8    $conn->query("set names utf8");     $result= $conn->query($sql) or die($conn->error);     $arr = array ();       $i=0;       while ($row = $result->fetch_assoc()) {         $arr[$i++] = $row;       }       $result->free();       //$conn->close();     return  $arr;   }        public function otherOperate($sql,$conn){   $conn->query("set names utf8");      if($conn->query($sql)){        if($conn->affected_rows>0){           return "1";        }else{           return "0";        }      }        }   //分页查询   public function findAll($pageSql, $totalPageSql, $fenyePage,$conn) {   $conn->query("set names utf8");   $result = $conn->query($pageSql);   $arr = array ();   $i=0;   while ($row = $result->fetch_assoc()) {   $arr[$i++] = $row;   }   $result->free();      //获取分页所需要的显示数据   $fenyePage->fenyeArray = $arr;   //获取总的数据行数   $res2 = $conn->query($totalPageSql) or die($this->conn->error);   if ($rows = $res2->fetch_row()) {   //获取总的页数   $fenyePage->sumPage = ceil($rows[0] / $fenyePage->everyPageRows);   }   //释放资源   $res2->free();      }      //循环删除数据   function loopDelete($sqls,$conn){   $conn->query("set names utf8");   $temp=0;   $flag="0";   for($i=0;$i<count($sqls);$i++){   if($conn->query($sqls[$i])){   if($conn->affected_rows>0){   $temp++;   }   }   }    if($temp>0){    $flag="1";    }else{    $flag="0";    }       return $flag;   }   }?>

3.比较简单的分页组件,虽然有点小瑕疵但还是很好用

<?phpclass fenyePage {public $everyPageRows; //每页显示的行数public $sumPage; //总页数public $nowPage; //当前所在页数public $fenyeArray; //分页显示的数据public $navigate; //分页导航条public $pageWhole; //翻页页数public $url; //翻页页数function showNavigate() {echo "<ul class='fenye_ul'>";echo "<a href='$this->url&nowPage=1'><li>首页</li></a>";if ($this->nowPage > 1) {echo "<a href='$this->url&nowPage=" . ($this->nowPage - 1) . "'><li class='btn'>上页</li></a>";}//翻页$startPage = floor(($this->nowPage-1) / $this->pageWhole) * $this->pageWhole + 1;$index = $startPage;//如果当前页是在1到10之间,就没有必要显示向前翻页的链接if ($this->nowPage > $this->pageWhole) {echo "<a href='$this->url&nowPage=" . ($startPage -1) . "'><li><b><<</b></li></a>";}for (; $startPage < $index + $this->pageWhole; $startPage++) {if ($startPage == $this->nowPage) {echo "<a href='$this->url&nowPage=$startPage'><li style='background:#6699cc;'>$startPage</li></a>";} else {echo "<a href='$this->url&nowPage=$startPage'><li>$startPage</li></a>";}}//如果startPage的值小于总的页数,就显示向后翻译if ($startPage < $this->sumPage) {echo "<a href='$this->url&nowPage=$startPage'><li><b>>></b></li></a>";}if ($this->nowPage < $this->sumPage) {echo "<a href='$this->url&nowPage=" . ($this->nowPage + 1) . "'><li>下页</li></a>";}echo "<a href='$this->url&nowPage={$this->sumPage}'><li>末页</li></a>";echo "<li>共{$this->sumPage}页</li>";echo "</ul>";}}?>

4.session操作类主要用于拦截未登录用户

<?php  session_start();  $user=$_SESSION["username"];  if(empty($user)){  header("Location:../../index.php");  exit();  }?>