我的BLOG开张了,做好准备喔!!!
php 分页类 可用于smarty
上一篇 /
下一篇 2008-03-24 00:45:14
/ 个人分类:技术文章
<?
#江南大学观方站系统
#数据库分页查徇类(适用于smarty模板调用)
#yangzhu2008-3-23
//include('../db_connect.php');
Class ShowPage {
Public $Page;//当前实际页码
Public $NowPage;//当前显示页码
Public $EvePage;//每页显示数据
Public $Cstart;
Public $PrePage;//前一页
Public $NextPage;//下一页
Public $CountPage;//总页数
Function GetCount($sql){//返回总页数
GLOBAL $dbh;
$res=mysql_query($sql);
$row=mysql_fetch_row($res);
$this->CountPage = ceil($row[0]/$this->EvePage);
}
Function GetArr($sql){//返回查徇的数组
GLOBAL $dbh;
if ( $this->Page == '' or $this->Page <=0 ) $this->Page =0;
if ( $this->Page >= $this->CountPage ) $this->Page =$this->Page - 1;
$this->Cstart = $this->Page * $this->EvePage;
$sql=$sql." limit $this->Cstart, $this->EvePage";
echo $sql;
$res=mysql_query($sql);
while( $row = mysql_fetch_assoc($res) ){
$arr[] = $row;
};
$this->PrePage = $this->Page - 1;
$this->NextPage = $this->Page + 1;
$this->NowPage = $this->Page + 1;
return $arr;
}
}
$MysqlPage = new showpage(); //创建分页类实例
$evepage=10; //每页显示
$MysqlPage->EvePage=$evepage;
$MysqlPage->Page=$page; //当前页
$sql = "select count(*) from pic";
$MysqlPage->GetCount($sql);
$sql = "select * from pic";
$arr = $MysqlPage->GetArr($sql); /*分页*/
$countpage = $MysqlPage->CountPage;
$nowpage = $MysqlPage->NowPage;
$prepage = $MysqlPage->PrePage;
$nextpage = $MysqlPage->NextPage;
?>
写了一个晚上才搞完这个分页,总要是想理解类,发现有点明白了...还要努力.感觉类的使用确实很方便,与函数相比个人感觉类的好处是可以返回很多的值,以前写过一个分页函数,返回数组后又要再查徇上一页下一页之类的东西.但类可以在处理类中的变量,在外部直接调用,相当于返回了很多值,也不知道理解的对不对,感觉应该是这样.
后面注解掉的是使用该类的方式.还有数据库连接语句,
smarty的使用如下.
{section name=item loop=$arr}
<tr>
<td><a href="id={$arr[item].pid}"></a></td>
<td>{$arr[item].tid}</td>
<td>{$arr[item].content}</td>
<td>{$arr[item].tag}</td>
<td>{$arr[item].email}</td>
<td>{$arr[item].addtime}</td>
<td>{$arr[item].pnums}</td>
<td>{$arr[item].dnums}</td>
<td>{$arr[item].chk}</td>
<td>{$arr[item].picurl}</td>
</tr>
{sectionelse} 对不起,没有任何对应数据!
{/section}
</table>
<div>共{$nowpage}/{$countpage}页 <a href=index.php?action=HotBang&page={$prepage}>上一页</a> <a href=index.php?
action=HotBang&page={$nextpage}>下一页</a> </div>
<!--end container......-->
{include file="footer.html"}
我写的东西暂时还没有怎么考虑效率..估计也不怎么样.慢慢来吧.
导入论坛
引用链接
收藏
分享给好友
推荐到圈子
管理
举报
TAG: