简单的PHP+SMARTY分页类

作者 : admin 于 2008年08月29日, 23:21:47
2008
08-29

类的代码

  1. <?php
  2. /*  Page.Class.php
  3.  *  Page
  4.  *  @link        http://www.sunboyu.cn
  5.  *  @package     OA
  6.  *  @version     V1.0
  7.  *
  8.  *  2008 08 28  sunboyu@gmail.com
  9.  */
  10.  class Page
  11.  {
  12. public $count;      #结果总数
  13. public $page;       #当前页
  14. public $pagesize;   #每页结果数
  15. public $pagecount;  #翻页数
  16. public $baseurl;    #url
  17. public $result;     #结果数组集
  18. public $pagelist;   #每翻页数
  19.  
  20. #构造函数,初始化变量
  21. function __construct( $count , $page , $pagesize , $pagelist , $baseurl = false )
  22. {
  23. $this->count     = $count;
  24. $this->page      = $page;
  25. $this->pagesize  = $pagesize;
  26. $this->baseurl   = isset($baseurl) ? $baseurl : $this->__geturl();
  27. $this->pagelist = $pagelist;
  28. }
  29.  
  30. #获得当前url
  31. function __geturl()
  32. {
  33. return ereg_replace("(^|&)page={$page}","",$_SERVER['QUERY_STRING']);
  34. }
  35.  
  36. #获得分页列表
  37. function __getpagelist()
  38. {
  39. $this->result['count'] = $this->count;
  40. $this->result['page'] = $this->page;
  41. $this->result['pagesize'] = $this->pagesize;
  42. $this->result['pagecount'] = ceil($this->count/$this->pagesize);
  43. if($this->result['pagecount']<=1) //只有一页以下
  44. {
  45. $this->result['pagelist'] = 0;
  46. }
  47. else //一页以上
  48. {
  49. #前一页,第一页的算法
  50. $this->result['first'] = ($this->page == 1) ? 0 : 1;
  51. $this->result['pre'] = ($this->page == 1) ? 0 : 1;
  52. #后一页,最后一页的算法
  53. $this->result['next'] = ($this->page == $this->pagecount ) ? 0 : 1;
  54. $this->result['last'] = ($this->page == $this->pagecount ) ? 0 : 1;
  55.  
  56.  
  57. #起始
  58. $pagearray = array();
  59. $start = floor(($this->page-1)/10)*10+1;
  60. for($i=0;$i<10;$i++)
  61. {
  62.      if( ($start+$i) <= $this->result['pagecount'])
  63. {
  64.      $pagearray[$i]['page'] = $start+$i;
  65. }
  66. if( ($start+$i) != $this->page )
  67. {
  68.      $pagearray[$i]['link'] = 1;
  69. }
  70. }
  71. #分页导航列表
  72. $this->result['pagelist'] = $pagearray;
  73. $this->result['baseurl'] = $this->baseurl;
  74. }
  75. }
  76. }
  77. ?>

模版代码

  1. <table>
  2.     <tr>
  3.     <td>
  4. 共{{$page.count}}条数据 每页{{$page.pagesize}}条 共{{$page.pagecount}}页 当前第{{$page.page}}页
  5. </td>
  6. <td>
  7. {{if $page.first eq 1}}
  8. <a href="{{$page.baseurl}}">首页</a>
  9. {{else}}
  10. 首页
  11. {{/if}}
  12. {{if $page.pre eq 1}}
  13. <a href="{{$page.baseurl}}&page={{$page.page-1}}">上一页</a>
  14. {{else}}
  15. 上一页
  16. {{/if}}
  17. {{foreach from=$page.pagelist item=vols}}
  18.     {{if $vols.link eq 1}}
  19. <a href="{{$page.baseurl}}&page={{$vols.page}}">[{{$vols.page}}]</a>
  20. {{else}}
  21. [{{$vols.page}}]
  22. {{/if}}
  23. {{/foreach}}
  24. {{if $page.next eq 1}}
  25. <a href="{{$page.baseurl}}&page={{$page.page-1}}">下一页</a>
  26. {{else}}
  27. 下一页
  28. {{/if}}
  29. {{if $page.last eq 1}}
  30. <a href="{{$page.baseurl}}&page={{$page.pagecount}}">尾页</a>
  31. {{else}}
  32. 尾页
  33. {{/if}}
  34. </td>
  35. </tr>
  36. </table>

调用范例

  1. <?php
  2. require_once('Include/Init.inc.php');
  3. require_once('Include/Class/Page.Class.php');
  4. $page = new Page( 999 , 12 , 10 , 6 , '?' );
  5. $page->__getpagelist();
  6. $smarty->assign("page",$page->result);
  7. $smarty->display("page.html");
  8. ?>