一个PHP使用odbc连接mssql的分页类,可直接输出分页结果,可 控制每页显示多少条记录,类中定义和设置功能都挺多,如果你的PHP连接的是MS SQLSERVER,在分页时候或许可以尝试下本代码:
001 |
<? |
002 |
//PHP基于DSN的数据库分页 |
003 |
class Pages{ |
004 |
var $cn ; //数据库游标 |
005 |
var $d ; //数据表的游标 |
006 |
var $result ; |
007 |
var $dsn ; //dsn源 |
008 |
var $user ; //DSN用户名 |
009 |
var $pass ; //DSN密码 |
010 |
var $total ; //记录总数 |
011 |
var $pages ; //总页数 |
012 |
var $onepage ; //每页条数 |
013 |
var $page ; //当前页 |
014 |
var $fre ; //上一页 |
015 |
var $net ; //下一页 |
016 |
var $i ; //控制每页显示 |
017 |
function getConnect( $dsn , $user , $pass ){ |
018 |
$this ->cn=@odbc_connect( $dsn , $user , $pass ); |
019 |
if (! $this ->cn){ |
020 |
$error = "不能连接数据库" ; |
021 |
$this ->getMess( $error ); |
022 |
} |
023 |
} |
024 |
function getDo( $sql ){ //从表中查询数据 |
025 |
$this ->d=@odbc_do( $this ->cn, $sql ); |
026 |
if (! $this ->d){ |
027 |
$error = "查询时发生了小错误......" ; |
028 |
$this ->getMess( $error ); |
029 |
} |
030 |
return $this ->d; |
031 |
} |
032 |
function getTotal( $sql ){ |
033 |
$this ->sql= $sql ; |
034 |
$dT = $this ->getDo( $this ->sql); //总数游标 |
035 |
$this ->total=odbc_result( $dT , 'total' ); //这里为何不能$this->d呢? |
036 |
return $this ->total; |
037 |
} |
038 |
function getList( $sql , $onepage , $page ){ |
039 |
$this ->s= $sql ; |
040 |
$this ->onepage= $onepage ; |
041 |
$this ->page= $page ; |
042 |
$this ->dList= $this ->getDo( $this ->s); //表游标 |
043 |
$this ->pages= ceil ( $this ->total/ $this ->onepage); |
044 |
if ( $this ->pages==0) |
045 |
$this ->pages++; //不能取到第0页 |
046 |
if (!isset( $this ->page)) |
047 |
$this ->page=1; |
048 |
$this ->fre
= $this ->page-1; //显示的页数 |
049 |
$this ->nxt
= $this ->page+1; |
050 |
$this ->nums=( $this ->page-1)* $this ->onepage; |
051 |
return $this ->dList; |
052 |
} |
053 |
function getFanye(){ |
054 |
$str = "" ; |
055 |
if ( $this ->page!=1) |
056 |
$str .= "<a
href=" . $PHP_SELF . "?page=1>
首页 </a><a href=" . $PHP_SELF . "?page=" . $this ->fre. ">
前页 </a>" ; |
057 |
else |
058 |
$str .= "<font
color=999999>首页 前页</font>" ; |
059 |
if ( $this ->page< $this ->pages) |
060 |
$str .= "<a
href=" . $PHP_SELF . "?page=" . $this ->nxt. ">
后页 </a>" ; |
061 |
else |
062 |
$str .= "<font
color=999999> 后页 </font>" ; |
063 |
if ( $this ->page!= $this ->pages) |
064 |
$str .= "<a
href=" . $PHP_SELF . "?page=" . $this ->pages. ">
尾页 </a>" ; |
065 |
else |
066 |
$str .= "<font
color=999999> 尾页 </font>" ; |
067 |
$str .= "共" . $this ->pages. "页" ; |
068 |
$str .= "您正浏览第<font
color=red>" . $this ->page. "</font>页" ; |
069 |
return $str ; |
070 |
} |
071 |
function getNums(){ |
072 |
return $this ->nums; |
073 |
} |
074 |
function getOnepage(){ //每页实际条数 |
075 |
return $this ->onepage; |
076 |
} |
077 |
function getI(){ |
078 |
return $this ->i; |
079 |
} |
080 |
function getPage(){ |
081 |
return $this ->page; |
082 |
} |
083 |
function getMess( $error ){ //定制消息 |
084 |
echo "<center>$error</center>" ; |
085 |
exit ; |
086 |
} |
087 |
} |
088 |
$pg = new Pages(); |
089 |
$pg ->getConnect( "lei" , "sa" , "star" ); |
090 |
$pg ->getTotal( "select
count(*) as total from article" ); //表求总数,注意,实际用这里需要改成你自己的表 |
091 |
$pg ->getList( "select
xs_name from article order by xs_id" ,8, $page ); //这里用时也要改成自己的表 |
092 |
if ( $pg ->getNums()!=0){ |
093 |
for ( $i =0; $i < $pg ->getNums();odbc_fetch_row( $pg ->dList), $i ++); |
094 |
} |
095 |
$i =0; |
096 |
while (odbc_fetch_row( $pg ->dList)){ |
097 |
$name =odbc_result( $pg ->dList, "xs_name" ); |
098 |
echo $name . "<br>" ; |
099 |
if ( $i == $pg ->getOnepage()){ //结束循环 |
100 |
break ; |
101 |
} |
102 |
$i ++; |
103 |
} |
104 |
//输出分页结果 |
105 |
echo $pg ->getFanye(); |
106 |
?> |