1 <?php 2 //數據庫連接類 3 class ConnDB{ 4 5 var $dbtype; 6 var $host; 7 var $user; 8 var $pwd; 9 var $dbname; 10 11 //構造方法 12 function ConnDB($dbtype,$host,$user,$pwd,$dbname){ 13 $this->dbtype=$dbtype; 14 $this->host=$host; 15 $this->user=$user; 16 $this->pwd=$pwd; 17 $this->dbname=$dbname; 18 } 19 20 //實現數據庫的連接並返回連接對象 21 function GetConnId(){ 22 23 if($this->dbtype=="mysql" || $this->dbtype=="mssql"){ 24 $dsn="$this->dbtype:host=$this->host;dbname=$this->dbname"; 25 }else{ 26 $dsn="$this->dbtype:dbname=$this->dbname"; 27 } 28 try { 29 $conn = new PDO($dsn, $this->user, $this->pwd); //初始化一個PDO對象,就是創建了數據庫連接對象$pdo 30 $conn->query("set names utf8"); 31 return $conn; 32 } catch (PDOException $e) { 33 die ("Error!: " . $e->getMessage() . "<br/>"); 34 } 35 36 37 } 38 } 39 40 //數據庫管理類 41 class AdminDB{ 42 43 44 function ExecSQL($sqlstr,$conn){ 45 46 $sqltype=strtolower(substr(trim($sqlstr),0,6)); 47 $rs=$conn->prepare($sqlstr); //准備查詢語句 48 $rs->execute(); //執行查詢語句,並返回結果集 49 if($sqltype=="select"){ 50 $array=$rs->fetchAll(PDO::FETCH_ASSOC); //獲取結果集中的所有數據 51 if(count($array)==0 || $rs==false) 52 return false; 53 else 54 return $array; 55 }elseif ($sqltype=="update" || $sqltype=="insert" || $sqltype=="delete"){ 56 if($rs) 57 return true; 58 else 59 return false; 60 } 61 } 62 } 63 //分頁類 64 class SepPage{ 65 var $rs; 66 var $pagesize; 67 var $nowpage; 68 var $array; 69 var $conn; 70 var $sqlstr; 71 function ShowData($sqlstr,$conn,$pagesize,$nowpage){ //定義方法 72 if(!isset($nowpage) || $nowpage=="") //判斷變量值是否為空 73 $this->nowpage=1; //定義每頁起始頁 74 else 75 $this->nowpage=$nowpage; 76 $this->pagesize=$pagesize; //定義每頁輸出的記錄數 77 $this->conn=$conn; //連接數據庫返回的標識 78 $this->sqlstr=$sqlstr; //執行的查詢語句 79 $offset=($this->nowpage-1)*$this->pagesize; 80 $sql=$this->sqlstr." limit $offset, $this->pagesize"; 81 $result=$this->conn->prepare($sql); //准備查詢語句 82 $result->execute(); //執行查詢語句,並返回結果集 83 $this->array=$result->fetchAll(PDO::FETCH_ASSOC); //獲取結果集中的所有數據 84 if(count($this->array)==0 || $this->array==false) 85 return false; 86 else 87 return $this->array; 88 } 89 90 function ShowPage($contentname,$utits,$anothersearchstr,$anothersearchstrs,$class){ 91 $str=""; 92 $res=$this->conn->prepare($this->sqlstr); //准備查詢語句 93 $res->execute(); //執行查詢語句,並返回結果集 94 $this->array=$res->fetchAll(PDO::FETCH_ASSOC); //獲取結果集中的所有數據 95 $record=count($this->array); //統計記錄總數 96 97 $pagecount=ceil($record/$this->pagesize); //計算共有幾頁 98 $str.=$contentname." ".$record." ".$utits." 每頁 ".$this->pagesize." ".$utits." 第 ".$this->nowpage." 頁/共 ".$pagecount." 頁"; 99 $str.=" "; 100 if($this->nowpage!=1) 101 $str.="<a href=".$_SERVER['PHP_SELF']."?page=1&page_type=".$anothersearchstr."¶meter2=".$anothersearchstrs." class=".$class.">首頁</a>"; 102 else 103 $str.="<font color='#555555'>首頁</font>"; 104 $str.=" "; 105 if($this->nowpage!=1) 106 $str.="<a href=".$_SERVER['PHP_SELF']."?page=".($this->nowpage-1)."&page_type=".$anothersearchstr."¶meter2=".$anothersearchstrs." class=".$class.">上一頁</a>"; 107 else 108 $str.="<font color='#555555'>上一頁</font>"; 109 $str.=" "; 110 if($this->nowpage!=$pagecount) 111 $str.="<a href=".$_SERVER['PHP_SELF']."?page=".($this->nowpage+1)."&page_type=".$anothersearchstr."¶meter2=".$anothersearchstrs." class=".$class.">下一頁</a>"; 112 else 113 $str.="<font color='#555555'>下一頁</font>"; 114 $str.=" "; 115 if($this->nowpage!=$pagecount) 116 $str.="<a href=".$_SERVER['PHP_SELF']."?page=".$pagecount."&page_type=".$anothersearchstr."¶meter2=".$anothersearchstrs." class=".$class.">尾頁</a>"; 117 else 118 $str.="<font color='#555555'>尾頁</font>"; 119 if(count($this->array)==0 || $this->array==false) 120 return "無數據!"; 121 else 122 return $str; 123 } 124 } 125 //系統常用方法 126 class UseFun{ 127 128 function UnHtml($text){ 129 $content=(nl2br(htmlspecialchars($text))); 130 $content=str_replace("[strong]","<strong>",$content); 131 $content=str_replace("[/strong]","</strong>",$content); 132 $content=str_replace("[em]","<em>",$content); 133 $content=str_replace("[/em]","</em>",$content); 134 $content=str_replace("[u]","<u>",$content); 135 $content=str_replace("[/u]","</u>",$content); 136 137 138 $content=str_replace("[font color=#FF0000]","<font color=#FF0000>",$content); 139 $content=str_replace("[font color=#00FF00]","<font color=#00FF00>",$content); 140 $content=str_replace("[font color=#0000FF]","<font color=#0000FF>",$content); 141 142 $content=str_replace("[font face=楷體_GB2312]","<font face=楷體_GB2312>",$content); 143 $content=str_replace("[font face=宋體]","<font face=新宋體>",$content); 144 $content=str_replace("[font face=隸書]","<font face=隸書>",$content); 145 $content=str_replace("[/font]","</font>",$content); 146 //$content=str_replace(chr(32)," ",$content); 147 $content=str_replace("[font size=1]","<font size=1>",$content); 148 $content=str_replace("[font size=2]","<font size=2>",$content); 149 $content=str_replace("[font size=3]","<font size=3>",$content); 150 $content=str_replace("[font size=4]","<font size=4>",$content); 151 $content=str_replace("[font size=5]","<font size=5>",$content); 152 $content=str_replace("[font size=6]","<font size=6>",$content); 153 154 $content=str_replace("[FIELDSET][LEGEND]","<FIELDSET><LEGEND>",$content); 155 $content=str_replace("[/LEGEND]","</LEGEND>",$content); 156 $content=str_replace("[/FIELDSET]","</FIELDSET>",$content); 157 return $content; 158 } 159 160 } 161 162 ?>