<?php 
  /*
    类名称:smarty分页类
    类属性：$sql:sql语句
	        $pageSize:设置每页显示数
			$url：查询分页的url地址
			$page：当前的页的数目
			$conn：数据库连接Id
     类方法：__construction():功能：初始化数据
	                          返回：null
	        getPage():功能：获得分页数据
			          返回：$row,array()类型，查询获得的数据
            pageBar()：功能：获得分页用的工具栏
			           返回：$pageBar，string类型，显示分页工具栏
			 __destruct():功能：类销毁数据库连接
			 返回：null          
	  类的使用：
	       1.php页面调用：   
	          require_once('conn.php');//包含数据库连接的Id
              require_once('smarty.php');//包含Smarty配置文件
	          require_once('page.class.php');//包含smarty分页类
              $pager=new page($sql,$pageSize,$url,$page,$conn);//实例化类
              $smarty->assign('row',$pager->getPage());//获得分页数据
              $smarty->assign('pageBar',$pager->pageBar());//显示分页工具栏
              $smarty->display('catList.html');//在模板页显示		
            2、模板页调用
            {section name=n loop=$row start=0 step=1}
			<div class="post">
				<h2 class="title"><a href="show.php?newsId={$row[n].newsId}">{$row[n].title}  </a></h2>
				<p class="meta"><span class="date">{$row[n].addtime}</span><span class="posted">作者：{$row[n].author}</span></p>
			</div>
			{/section}
			<br>
			{$pageBar} 
  */
  class page{
  	public  $sql;
  	public  $pageSize;
  	public  $url;
  	public  $page;
  	public  $conn;
  	public  function __construct($sql,$pageSize,$url,$page,$conn){
  		$this->sql=$sql;
  		$this->pageSize=$pageSize;
  		$this->url=$url;
  		$this->page=$page;
  		$this->conn=$conn;
  	}
  	
  	public  function getPage(){
  		$result=mysql_query($this->sql,$this->conn);
  		$totalNum=mysql_num_rows($result);
  		$pageNum=ceil($totalNum/$this->pageSize);
  		$start=($this->page-1)*$this->pageSize;
  	    $end=$this->pageSize;
  		$sql=$this->sql." limit $start,$end";
  		$result1=mysql_query($sql);
  		while ($row1=mysql_fetch_array($result1,MYSQL_ASSOC)) {
  			$row[]=$row1;
  		}
  		foreach ($row	as $key=>$value){
	     $row[$key]=$value;
        }
  		return $row;
  	}
  	public function pageBar(){
  	
  		$result=mysql_query($this->sql,$this->conn);
  		$totalNum=mysql_num_rows($result);
  		$pageNum=ceil($totalNum/$this->pageSize);
  		$homePage="<a href=\"".$this->url."page=1\">首页<a/>";
  		if($this->page==1){
  			$prepage=null;
  		}else{
  			$prepage1=$this->page-1;
  			$prepage="<a href=\"".$this->url."page=$prepage1\">上一页</a>";
  		}
  		if($this->page==$pageNum){
  			$nextPage=null;
  		}else{
  			$nextPage1=$this->page+1;
  			$nextPage="<a href=\"".$this->url."page=$nextPage1\">下一页</a>";
  		}
  		$endPage="<a href=\"".$this->url."page=$pageNum\">最后一页</a>";
  		$pageBar="<span>总共&nbsp;$pageNum&nbsp;页</span>&nbsp;&nbsp;<span>当前&nbsp;$this->page&nbsp;页</span>&nbsp;&nbsp;<span>$homePage</span>&nbsp;&nbsp;<span>$prepage</span>&nbsp;&nbsp;<span>$nextPage</span>&nbsp;&nbsp;<span>$endPage</span>";
  	    return $pageBar;
  	}
  	public function __destruct(){
  		mysql_close($this->conn);
  	}
  }
?>