piaofengnet
中级会员
 
UID 82620
精华
0
积分 230
帖子 106
金钱 230 喜悦币
威望 0
人脉 0
阅读权限 30
注册 2006-11-4
状态 离线
|
数据操作类和分页类一起(欢迎指教批评)
这个是我自己写的数据操作类和分页类!
请大家给点意见!
如果觉得好请加精华!!
<?php /* 程序设计: 林建炫(飘枫设计室) made in 广州 by 2006-10-26 晚 QQ: 5818500 Email: [email]linzhenxuan@163.com[/email] 尊重他人也尊重自己!请勿删除该版权信息 如果你使用、修改请保留该信息! */ include_once ("config.php"); include_once ("cls_conn.php"); //$isdb= '1mysql,2mssql //$pconnect = '1永久,0关 //$charset = 'gb2312 gbk utf-8 Class Cls_db { var $acpath; var $sqlnum=0; var $isdb; var $pagesize = 20; var $page; var $recordcount; var $pagecount; var $conn; Function __construct() { global $dbserver,$dbuser,$dbpassword,$dbname,$charset,$pconnect,$isdb,$acpath; $this->conndb($dbserver,$dbuser,$dbpassword,$dbname,$charset,$pconnect,$isdb,$acpath); if ($_POST['page']!="") { $this->page=$_POST['page']; } else { $this->page=$_GET['page']; } if (empty($this->page)){$this->page=1;} if (!is_numeric($this->page)){$this->page=1;} }//初始化了数据库连接和page的值 Function __destruct() { $this->close(); } Function conndb($dbserver,$dbuser,$dbpassword,$dbname,$charset='',$pconnect=0,$isdb=1,$acpath="") { $this->isdb=$isdb; if($isdb==1)//mysql { if ($pconnect==1) { mysql_pconnect($dbserver,$dbuser,$dbpassword) or die ("connection database is error!!"); } else { mysql_connect($dbserver,$dbuser,$dbpassword) or die ("connection database is error!!"); } if (empty($charset)) { $charset = "Gb2312"; } if ($charset) { mysql_query("SET NAMES Gb2312"); } mysql_select_db($dbname) or die("select database name is error"); }//End if if($isdb==2) { if ($pconnect==1) { mssql_pconnect($dbserver,$dbuser,$dbpassword) or die ("connection database is error!!"); } else { mssql_connect($dbserver,$dbuser,$dbpassword) or die ("connection database is error!!"); } mssql_select_db($dbname) or die("select database name is error"); } if($isdb==3) {//Access connection $this->conn = new conn; }//End function }//end function Function acopen($sql) { global $conn; $conn = @new COM("ADODB.Connection") or die ("ADO连接失败!"); $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($this->acpath); $conn->Open($connstr); return $conn->execute($sql); } Function query($query,$mthod=1) { global $isdb; $this->sqlnum++; if ($isdb==1) { //LIMIT 0 , 10 //分页中 if(substr($query,0,6)=="select" && $mthod==1) { //求总页数 $temp = mysql_query($query); //改变sql语句 $query=$query." limit ".($this->page-1)*$this->pagesize." , ".$this->pagesize; $this->num_rows($temp); } $result = mysql_query($query); return $result; } if ($isdb==2) { return mssql_query($query); } if($isdb==3) { return $this->conn->execute($query); } } Function fetch_row($query) { Global $isdb; //设置了info很好的提供了出错信息! $info = false; if ($isdb==1) { if(!$info = mysql_fetch_row($query)) { $info = false; } return $info; } if ($isdb==2) { return mssql_fetch_row($query); } } Function fetch_array($query) { Global $isdb; if ($isdb==1) { return mysql_fetch_array($query); } if ($isdb==3) { return $this->conn->GetArray($query); } } Function insert_id() { return mysql_insert_id(); } Function num_rows($query) { Global $isdb; if ($isdb==1) { $this->recordcount = mysql_num_rows($query);//给$recordcount值 $this->pagecount = ceil($this->recordcount/$this->pagesize); if ($this->page>$this->pagecount){$this->page=$this->pagecount;} } } Function free_result($query) { Global $isdb; if($isdb==1) { mysql_free_result($query); } } Function close() { if ($this->isdb==1) { mysql_close(); } if ($this->isdb==2) { mssql_close(); } } //以下是分页信息 Function linkurl()//地址栏上多余的信息 { if($_GET) { $get = $_GET; } if($_POST) { $get = $_POST; } if(!$_POST && !$_GET) { $get = $_GET; } $i = 0; foreach ($get as $key=>$a) { if ($key!="page"){ if ($i==0){ $result = $key."=".$a; } else { $result= $result."&".$key."=".$a; } $i++; } } return $result; } //正式分页 样式1: 页次 1 / 2 页 共 37 条记录 30 条/页 分页:<< < 1 > >> 转到: Function page_one() { //类型 页次 1 / 2 页 共 37 条记录 30 条/页 分页:<< < 1 > >> 转到: $pagesize = $this->pagesize; $pagecount = $this->pagecount; $recordcount = $this->recordcount; $page = $this->page; $temp = "页次 $page / $pagecount 页 共 $recordcount 条记录 $pagesize 条/页 "; $url = $this->linkurl(); if (!empty($url)) { $url = "&".$url; } $temp = $temp." 分页:<a href='?page=1$url'><font title face='webdings' dypop='第一页'>9</font></a>"; if($page>1) { $temp = $temp." <a href='?page=".($page-1)."$url'><font title face='webdings' dypop='上一页'>7</font></a> "; } else { $temp = $temp." <font title face='webdings' dypop='上一页'>7</font> "; } $temp = $temp."<b>$page</b>"; if ($page<$pagecount) { $temp = $temp." <a href='?page=".($page+1)."$url'><font title face='webdings' dypop='下一页'>8</font></a> "; } else { $temp = $temp." <font title face='webdings' dypop='下一页'>8</font> "; } $temp = $temp."<a href='?page=$pagecount$url'><font title face='webdings' dypop='最后一页'>:</font></a>"; $temp = $temp." 转到:<select size='1' name='page' onchange=\"javascript:window.location='?page='+this.options[this.selectedIndex].value+'$url'\">"; for($i=1;$i<=$pagecount;$i++) { if($_GET['page']==$i) { $temp = $temp."<option value=$i selected>第".$i."頁</option>"; } else { $temp = $temp."<option value=$i>第".$i."頁</option>"; } } $temp = $temp."</select>"; return $temp; } //正式分页 样式2(没美化): 500000 30 1/35041页 << < 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 > >>转到: Function page_two($total=10,$bgcolor="#FFFBE8",$border="1px",$solid="#E4E4E4") { $style="border: $border solid $solid"; $pagesize = $this->pagesize; $pagecount = $this->pagecount; $recordcount = $this->recordcount; $page = $this->page; $temp = "页次 $page / $pagecount 页 共 $recordcount 条记录 $pagesize 条/页 "; $url = $this->linkurl(); if (!empty($url)) { $url = "&".$url; } $temp = "<table border='0' width='100%' id='table6' cellspacing='0' cellpadding='0' height='20'><tr>"; $temp .= "<td width='43' bgcolor='$bgcolor' style='$style' align='center'>$recordcount</td>"; $temp .= " <td width='38' bgcolor='$bgcolor' style='$style' align='center'>$pagesize</td> <td width='89' bgcolor='$bgcolor' style='$style' align='center'>$page / ".$pagecount."页</td><td style='$style'>"; if($page>1){$temp = $temp." <a href='?page=1$url'><font title face='webdings' dypop='第一页'>9</font></a> ";} if($page>1) { $temp = $temp." <a href='?page=".($page-1)."$url'><font title face='webdings' dypop='上一页'>7</font></a> "; } if($page==1 | $page==2) { for($i=1;$i<=$pagecount && $i<=$total;$i++) { if($i==1) { if($page==1 && $i==1) { $temp.=" <b>1</b> "; } else { $temp = $temp." <a href='?page=1$url'>1</a>"; } } else { if($page==2 && $i==2) { $temp.=" | <b>2</b> "; } else { $temp = $temp." | "."<a href='?page=$i$url'>$i</a>"; } } } } if ($page>2) { $a = $page-2; $b = $page+($total-3); for($i=$a;$i<=$pagecount && $i<=$b;$i++) { if($i==$a) { if($page==$i) { $temp.="<b>$a</b>"; } else { $temp = $temp." <a href='?page=$a$url'>$a</a>"; } } else { if($page==$i) { $temp.=" | <b>$i</b>"; } else { $temp = $temp." | "."<a href='?page=$i$url'>$i</a>"; } } } } if($page<$pagecount) { $temp.=" <a href='?page=".($page+1)."$url'><font title face='webdings' dypop='下一页'>8</font></a> "; } if($page<$pagecount){$temp.=" <a href='?page=$pagecount$url'><font title face='webdings' dypop='最后一页'>:</font></a> ";} $temp = $temp." <select size='1' name='page' onchange=\"javascript:window.location='?page='+this.options[this.selectedIndex].value+'$url'\">"; for($i=1;$i<=$pagecount;$i++) { if($_GET['page']==$i) { $temp = $temp."<option value=$i selected>第".$i."頁</option>"; } else { $temp = $temp."<option value=$i>第".$i."頁</option>"; } } $temp = $temp."</select></td></table>"; return $temp; } } /*已经注释掉了 $db = New cls_db; $db->pagesize=5; $query = $db->query("select username from user"); while ($row = $db->fetch_array($query)) { echo $row['username'].chr(10)."<br>"; } echo $db->sqlnum; echo $db->recordcount; echo "<br>"; echo $db->page_one(); echo "<br>"; echo $db->page_two(); $db->close(); unset($db); */ ?>
|
|