为了更好的开发,这次项目我写了几个比较有用的工具类主要就围绕数据库操作、分页、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 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 "";
echo "url&nowPage=1'>
if ($this->nowPage > 1) {
echo "url&nowPage=" . ($this->nowPage - 1) . "'>
}
//翻页
$startPage = floor(($this->nowPage-1) / $this->pageWhole) * $this->pageWhole + 1;
$index = $startPage;
//如果当前页是在1到10之间,就没有必要显示向前翻页的链接
if ($this->nowPage > $this->pageWhole) {
echo "url&nowPage=" . ($startPage -1) . "'>
}
for (; $startPage pageWhole; $startPage++) {
if ($startPage == $this->nowPage) {
echo "url&nowPage=$startPage'>
} else {
echo "url&nowPage=$startPage'>
}
}
//如果startPage的值小于总的页数,就显示向后翻译
if ($startPage sumPage) {
echo "url&nowPage=$startPage'>
}
if ($this->nowPage sumPage) {
echo "url&nowPage=" . ($this->nowPage + 1) . "'>
}
echo "url&nowPage={$this->sumPage}'>
echo "
echo "";
}
}
?>
4.session操作类主要用于拦截未登录用户
[php]
session_start();
$user=$_SESSION["username"];
if(empty($user)){
header("Location:../../index.php");
exit();
}
?>