简单的PHP+SMARTY分页类
作者 : admin 于 2008年08月29日, 23:21:47
2008
08-29
08-29
类的代码
- <?php
- /* Page.Class.php
- * Page
- * @link http://www.sunboyu.cn
- * @package OA
- * @version V1.0
- *
- * 2008 08 28 sunboyu@gmail.com
- */
- class Page
- {
- public $count; #结果总数
- public $page; #当前页
- public $pagesize; #每页结果数
- public $pagecount; #翻页数
- public $baseurl; #url
- public $result; #结果数组集
- public $pagelist; #每翻页数
- #构造函数,初始化变量
- function __construct( $count , $page , $pagesize , $pagelist , $baseurl = false )
- {
- $this->count = $count;
- $this->page = $page;
- $this->pagesize = $pagesize;
- $this->baseurl = isset($baseurl) ? $baseurl : $this->__geturl();
- $this->pagelist = $pagelist;
- }
- #获得当前url
- function __geturl()
- {
- return ereg_replace("(^|&)page={$page}","",$_SERVER['QUERY_STRING']);
- }
- #获得分页列表
- function __getpagelist()
- {
- $this->result['count'] = $this->count;
- $this->result['page'] = $this->page;
- $this->result['pagesize'] = $this->pagesize;
- $this->result['pagecount'] = ceil($this->count/$this->pagesize);
- if($this->result['pagecount']<=1) //只有一页以下
- {
- $this->result['pagelist'] = 0;
- }
- else //一页以上
- {
- #前一页,第一页的算法
- $this->result['first'] = ($this->page == 1) ? 0 : 1;
- $this->result['pre'] = ($this->page == 1) ? 0 : 1;
- #后一页,最后一页的算法
- $this->result['next'] = ($this->page == $this->pagecount ) ? 0 : 1;
- $this->result['last'] = ($this->page == $this->pagecount ) ? 0 : 1;
- #起始
- $pagearray = array();
- $start = floor(($this->page-1)/10)*10+1;
- for($i=0;$i<10;$i++)
- {
- if( ($start+$i) <= $this->result['pagecount'])
- {
- $pagearray[$i]['page'] = $start+$i;
- }
- if( ($start+$i) != $this->page )
- {
- $pagearray[$i]['link'] = 1;
- }
- }
- #分页导航列表
- $this->result['pagelist'] = $pagearray;
- $this->result['baseurl'] = $this->baseurl;
- }
- }
- }
- ?>
模版代码
- <table>
- <tr>
- <td>
- 共{{$page.count}}条数据 每页{{$page.pagesize}}条 共{{$page.pagecount}}页 当前第{{$page.page}}页
- </td>
- <td>
- {{if $page.first eq 1}}
- <a href="{{$page.baseurl}}">首页</a>
- {{else}}
- 首页
- {{/if}}
- {{if $page.pre eq 1}}
- <a href="{{$page.baseurl}}&page={{$page.page-1}}">上一页</a>
- {{else}}
- 上一页
- {{/if}}
- {{foreach from=$page.pagelist item=vols}}
- {{if $vols.link eq 1}}
- <a href="{{$page.baseurl}}&page={{$vols.page}}">[{{$vols.page}}]</a>
- {{else}}
- [{{$vols.page}}]
- {{/if}}
- {{/foreach}}
- {{if $page.next eq 1}}
- <a href="{{$page.baseurl}}&page={{$page.page-1}}">下一页</a>
- {{else}}
- 下一页
- {{/if}}
- {{if $page.last eq 1}}
- <a href="{{$page.baseurl}}&page={{$page.pagecount}}">尾页</a>
- {{else}}
- 尾页
- {{/if}}
- </td>
- </tr>
- </table>
调用范例
- <?php
- require_once('Include/Init.inc.php');
- require_once('Include/Class/Page.Class.php');
- $page = new Page( 999 , 12 , 10 , 6 , '?' );
- $page->__getpagelist();
- $smarty->assign("page",$page->result);
- $smarty->display("page.html");
- ?>