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

为了更好的开发,这次项目我写了几个比较有用的工具类主要就围绕数据库操作、分页、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.数据库操作类

<?php
class 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.比较简单的分页组件,虽然有点小瑕疵但还是很好用

<?php
class 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();
  }
?>



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值