html5中文学习网

您的位置: 首页 > 网络编程 > PHP编程 » 正文

角色权限管理系统(工具类部分)_PHP教程_编程技术

[ ] 已经帮助:人解决问题
为了更好的开发,这次项目我写了几个比较有用的工具类主要就围绕数据库操作、分页、session来弄的一起来看看吧
 
1.数据库连接工具类
 
[php]  
<?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]  
<?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] 
<?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]  
<?php  
  session_start();  
  $user=$_SESSION["username"];  
  if(empty($user)){  
    header("Location:../../index.php");  
    exit();  
  }  
?>  
 
KmxHTML5中文学习网 - HTML5先行者学习网
KmxHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助