刚学php ,秀一下自己写的数据库操作的类和分页类
时间:2008-06-14
来源:互联网
其中分页仿照discuz 写的,样式也是用的它的
复制PHP内容到剪贴板
<?php
class db
{
public $conn,$db,$table,$user,$host,$unm,$pwd;
public $res;
public $char;
public $linkType;
function __construct($linkType=false,$char="gb2312")
{
$this->linkType=$linkType;//设定连接类型
$this->char=$char;//设定连接校对字符
$this->db=DB;
$this->user=USER;
$this->host=HOST;
$this->unm=UNM;
$this->pwd=PWD;
if($this->linkType)
{
$this->conn=mysql_pconnect($this->host,$this->unm,$this->pwd) or die("Database connection failure");
}
else
{
$this->conn=mysql_connect($this->host,$this->unm,$this->pwd) or die("Database connection failure");
}
mysql_select_db($this->db);
mysql_query("set names ".$this->char);
}
function query($sql,$type="true")
{
//$type:默认的操作。 指代select操作
$this->res=mysql_query($sql) or die("SQL Statement error !Please check it again");
$row=$type?mysql_num_rows($this->res):mysql_affected_rows();
$result["res"]=$this->res;
$result["row"]=$row;
return $result;
}
//fetch()方法:获取所有的记录,并写入数组
function fetch($sql)
{
$res=self::query($sql);
while ($rs=mysql_fetch_array($res["res"]))
{
$result[]=$rs;
}
return $result;
}
//获取下一条记录
function fetchNext($filed,$currenID,$table)
{
$sql="select * from $table where $filed>$currenID limit 0,1 ";
return self::fetch($sql);
}
//获取前一条记录
function fetchPre($filed,$currenID,$table)
{
$sql="select * from $table where $filed<$currenID limit 0,1 ";
return self::fetch($sql);
}
}
class page extends db
{
public $currentPage,$totalRecord,$totalPage,$pageSize;
public $start;
public $flag;
public $sql;
function __construct($sql,$pagesize=5,$flag="page")
{
$this->sql=$sql;
$this->pageSize=$pagesize;
$this->flag=$flag;//设定翻页链接标识符
$row=parent::query($sql);
$this->totalRecord=$row["row"];
$this->totalPage=ceil($this->totalRecord/$this->pageSize);
$page=$_REQUEST[$this->flag];
if($page<0||$page=="")
{
$this->currentPage=1;
}
else
{
$page>$this->totalPage?$this->currentPage=$this->totalPage:$this->currentPage=$page;
}
$this->start=($this->currentPage-1)*$this->pageSize;
}
//显示分页列表
function show($page=10)
{
$str.='<div class="pages_btns"><div class="pages">';
$str.="<em> ".$this->totalRecord." </em>";
$pre=$this->currentPage-1;
if($pre!=0)
{
$str.='<a href=?'.$this->flag.'='.$pre.' class=next><<</a>';
}
if($this->currentPage>=$page)
{
if($this->totalPage-$this->currentPage<10)
{
$start=$this->currentPage-($this->currentPage%10);
$end=$this->totalPage;
}
else
{
$start=$this->currentPage-2;
$end=$start+$page-1;
}
}
else
{
$start=1;
$end=10;
}
for ($i=$start;$i<=$end;$i++)
{
if($i==$this->currentPage)
{
$str.="<strong>".$i."</strong>";
}
else
{
$str.="<a href=?".$this->flag."=$i>$i</a>";
}
}
$next=$this->currentPage+1;
$str.='<a href=?'.$this->flag.'='.$next.' class=next>>></a>';
$str.="<a href=?".$this->flag."=".$this->totalPage." class=last>...".$this->totalPage."</a>";
$str.="<kbd><input type='text' name='custompage' size='3' onkeydown=\"if(event.keyCode==13) {window.location='{$_SERVER['PHP_SELF']}?{$this->flag}='+this.value; return false;}\" /></kbd>";
$str.="</div></div>";
return $str;
}
}
?>
<style>
*{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
text-decoration:none;
}
.pages{
border:solid #000000 1px;
height:25px;
margin-top:2px;
background-color:#FFFFFF;
width:98%;
margin-left:2px;
}
.pages_btns { clear: both; width: 100%; padding:5px;
background-color:#CCCCCC }
.pages a, .pages strong, .pages em, .pages kbd, #multipage .pages em { float: left; padding: 0 8px; line-height:26px; width:auto; color:#000000; font-weight:800}
.pages a:hover { background-color: #FF0000; color: #FFFFFF; text-decoration:none}
.pages strong { font-weight: bold; color:#FF0000; background: #CAD9EA;}
.pages a.prev, .pages a.next { line-height: 24px; }
.pages a.next { padding: 0 15px;}
.pages kbd { border-left: 1px solid #CAD9EA; margin: 0; }
* html .pages kbd { padding: 1px 8px; }
.pages kbd input { border: 1px solid #0033FF; margin-top: 3px !important; * > margin-top: 1px !important; margin: 1px 4px 0 3px; padding: 0 2px; height: 17px; }
.pages kbd>input { margin-bottom: 2px; }
</style>
test.php
PHP代码:
<?php
class db
{
public $conn,$db,$table,$user,$host,$unm,$pwd;
public $res;
public $char;
public $linkType;
function __construct($linkType=false,$char="gb2312")
{
$this->linkType=$linkType;//设定连接类型
$this->char=$char;//设定连接校对字符
$this->db=DB;
$this->user=USER;
$this->host=HOST;
$this->unm=UNM;
$this->pwd=PWD;
if($this->linkType)
{
$this->conn=mysql_pconnect($this->host,$this->unm,$this->pwd) or die("Database connection failure");
}
else
{
$this->conn=mysql_connect($this->host,$this->unm,$this->pwd) or die("Database connection failure");
}
mysql_select_db($this->db);
mysql_query("set names ".$this->char);
}
function query($sql,$type="true")
{
//$type:默认的操作。 指代select操作
$this->res=mysql_query($sql) or die("SQL Statement error !Please check it again");
$row=$type?mysql_num_rows($this->res):mysql_affected_rows();
$result["res"]=$this->res;
$result["row"]=$row;
return $result;
}
//fetch()方法:获取所有的记录,并写入数组
function fetch($sql)
{
$res=self::query($sql);
while ($rs=mysql_fetch_array($res["res"]))
{
$result[]=$rs;
}
return $result;
}
//获取下一条记录
function fetchNext($filed,$currenID,$table)
{
$sql="select * from $table where $filed>$currenID limit 0,1 ";
return self::fetch($sql);
}
//获取前一条记录
function fetchPre($filed,$currenID,$table)
{
$sql="select * from $table where $filed<$currenID limit 0,1 ";
return self::fetch($sql);
}
}
class page extends db
{
public $currentPage,$totalRecord,$totalPage,$pageSize;
public $start;
public $flag;
public $sql;
function __construct($sql,$pagesize=5,$flag="page")
{
$this->sql=$sql;
$this->pageSize=$pagesize;
$this->flag=$flag;//设定翻页链接标识符
$row=parent::query($sql);
$this->totalRecord=$row["row"];
$this->totalPage=ceil($this->totalRecord/$this->pageSize);
$page=$_REQUEST[$this->flag];
if($page<0||$page=="")
{
$this->currentPage=1;
}
else
{
$page>$this->totalPage?$this->currentPage=$this->totalPage:$this->currentPage=$page;
}
$this->start=($this->currentPage-1)*$this->pageSize;
}
//显示分页列表
function show($page=10)
{
$str.='<div class="pages_btns"><div class="pages">';
$str.="<em> ".$this->totalRecord." </em>";
$pre=$this->currentPage-1;
if($pre!=0)
{
$str.='<a href=?'.$this->flag.'='.$pre.' class=next><<</a>';
}
if($this->currentPage>=$page)
{
if($this->totalPage-$this->currentPage<10)
{
$start=$this->currentPage-($this->currentPage%10);
$end=$this->totalPage;
}
else
{
$start=$this->currentPage-2;
$end=$start+$page-1;
}
}
else
{
$start=1;
$end=10;
}
for ($i=$start;$i<=$end;$i++)
{
if($i==$this->currentPage)
{
$str.="<strong>".$i."</strong>";
}
else
{
$str.="<a href=?".$this->flag."=$i>$i</a>";
}
}
$next=$this->currentPage+1;
$str.='<a href=?'.$this->flag.'='.$next.' class=next>>></a>';
$str.="<a href=?".$this->flag."=".$this->totalPage." class=last>...".$this->totalPage."</a>";
$str.="<kbd><input type='text' name='custompage' size='3' onkeydown=\"if(event.keyCode==13) {window.location='{$_SERVER['PHP_SELF']}?{$this->flag}='+this.value; return false;}\" /></kbd>";
$str.="</div></div>";
return $str;
}
}
?>
<style>
*{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
text-decoration:none;
}
.pages{
border:solid #000000 1px;
height:25px;
margin-top:2px;
background-color:#FFFFFF;
width:98%;
margin-left:2px;
}
.pages_btns { clear: both; width: 100%; padding:5px;
background-color:#CCCCCC }
.pages a, .pages strong, .pages em, .pages kbd, #multipage .pages em { float: left; padding: 0 8px; line-height:26px; width:auto; color:#000000; font-weight:800}
.pages a:hover { background-color: #FF0000; color: #FFFFFF; text-decoration:none}
.pages strong { font-weight: bold; color:#FF0000; background: #CAD9EA;}
.pages a.prev, .pages a.next { line-height: 24px; }
.pages a.next { padding: 0 15px;}
.pages kbd { border-left: 1px solid #CAD9EA; margin: 0; }
* html .pages kbd { padding: 1px 8px; }
.pages kbd input { border: 1px solid #0033FF; margin-top: 3px !important; * > margin-top: 1px !important; margin: 1px 4px 0 3px; padding: 0 2px; height: 17px; }
.pages kbd>input { margin-bottom: 2px; }
</style>
复制PHP内容到剪贴板
<?php
define("HOST","localhost");
define("UNM","root");
define("PWD","root");
define("DB","test");
require_once('db.class.php');
$db=new db();
$sql="select * from yy";
$page=new page($sql,10);
$sql.=" limit $page->start,$page->pageSize";
$rs = $db->fetch($sql);
?>
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#FF0000">
<tr>
<td height="25" bgcolor="#FFFFFF">ID</td>
<td bgcolor="#FFFFFF">UNM</td>
</tr>
<?php
for($i=0;$i<count($rs);$i++)
{
?>
<tr>
<td height="25" bgcolor="#FFFFFF"><?=$rs[$i][0]?></td>
<td bgcolor="#FFFFFF"><?=$rs[$i][1]?></td>
</tr>
<?php
}
?>
<tr>
<td height="25" colspan="2" bgcolor="#FFFFFF"><?=$page->show()?></td>
</tr>
</table>
[ 本帖最后由 dongxin1390008 于 2008-6-13 17:40 编辑 ] PHP代码:
<?php
define("HOST","localhost");
define("UNM","root");
define("PWD","root");
define("DB","test");
require_once('db.class.php');
$db=new db();
$sql="select * from yy";
$page=new page($sql,10);
$sql.=" limit $page->start,$page->pageSize";
$rs = $db->fetch($sql);
?>
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#FF0000">
<tr>
<td height="25" bgcolor="#FFFFFF">ID</td>
<td bgcolor="#FFFFFF">UNM</td>
</tr>
<?php
for($i=0;$i<count($rs);$i++)
{
?>
<tr>
<td height="25" bgcolor="#FFFFFF"><?=$rs[$i][0]?></td>
<td bgcolor="#FFFFFF"><?=$rs[$i][1]?></td>
</tr>
<?php
}
?>
<tr>
<td height="25" colspan="2" bgcolor="#FFFFFF"><?=$page->show()?></td>
</tr>
</table>
作者: dongxin1390008 发布时间: 2008-06-13

作者: luzhou 发布时间: 2008-06-15
支持一下
作者: enigma1223 发布时间: 2008-06-19
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28