AjaxPages.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
  10. // +----------------------------------------------------------------------
  11. /*
  12. * PHP分页类
  13. * 修改者:白俊遥
  14. * 日 期:2015.5.10
  15. * 邮 箱:baijunyao@baijunyao.com
  16. * 博 客:http://www.baijunyao.com
  17. */
  18. namespace Think;
  19. class AjaxPages{
  20. public $firstRow; // 起始行数
  21. public $listRows; // 列表每页显示行数
  22. public $parameter; // 分页跳转时要带的参数
  23. public $totalRows; // 总行数
  24. public $totalPages; // 分页总页面数
  25. public $rollPage = 5;// 分页栏每页显示的页数
  26. public $lastSuffix = true; // 最后一页是否显示总页数
  27. private $p = 'p'; //分页参数名
  28. private $url = ''; //当前链接URL
  29. private $nowPage = 1;
  30. // 分页显示定制
  31. private $config = array(
  32. 'header' => '',//<span class="rows">共 %TOTAL_ROW% 条记录</span>
  33. 'first' => '首页',
  34. 'prev' => '上一页',
  35. 'next' => '下一页',
  36. 'last' => '末页',
  37. 'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%',
  38. );
  39. /**
  40. * 架构函数
  41. * @param array $totalRows 总的记录数
  42. * @param array $listRows 每页显示记录数
  43. * @param array $parameter 分页跳转的参数
  44. */
  45. public function __construct($totalRows, $listRows=20, $parameter = array()) {
  46. C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称
  47. /* 基础设置 */
  48. $this->totalRows = $totalRows; //设置总记录数
  49. $this->listRows = $listRows; //设置每页显示行数
  50. $this->parameter = empty($parameter) ? $_GET : $parameter;
  51. $this->nowPage = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
  52. $this->nowPage = $this->nowPage>0 ? $this->nowPage : 1;
  53. $this->firstRow = $this->listRows * ($this->nowPage - 1);
  54. }
  55. /**
  56. * 定制分页链接设置
  57. * @param string $name 设置名称
  58. * @param string $value 设置值
  59. */
  60. public function setConfig($name,$value) {
  61. if(isset($this->config[$name])) {
  62. $this->config[$name] = $value;
  63. }
  64. }
  65. /**
  66. * 生成链接URL
  67. * @param integer $page 页码
  68. * @return string
  69. */
  70. private function url($page){
  71. return str_replace(urlencode('[PAGE]'), $page, $this->url);
  72. }
  73. /**
  74. * 组装分页链接
  75. * @return string
  76. */
  77. public function show() {
  78. if(0 == $this->totalRows) return '';
  79. /* 生成URL */
  80. $this->parameter[$this->p] = '[PAGE]';
  81. $this->url = U(ACTION_NAME, $this->parameter);
  82. /* 计算分页信息 */
  83. $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数
  84. if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
  85. $this->nowPage = $this->totalPages;
  86. }
  87. /* 计算分页零时变量 */
  88. $now_cool_page = $this->rollPage/2;
  89. $now_cool_page_ceil = ceil($now_cool_page);
  90. //上一页
  91. $up_row = $this->nowPage - 1;
  92. $up_page = $up_row > 0 ? '<a class="prev" href="javascript:voidx(' .$up_row. ')">' . $this->config['prev'] . '</a>' : '<a class="prev" href="javascript:;">' . $this->config['prev'] . '</a>';
  93. //下一页
  94. $down_row = $this->nowPage + 1;
  95. $down_page = ($down_row <= $this->totalPages) ? '<a class="next" href="javascript:voidx('.$down_row. ')">' . $this->config['next'] . '</a>' : '<a class="next" href="javascript:;">' . $this->config['next'] . '</a>';
  96. //第一页
  97. $the_first = '';//'<a class="first" href="javascript:voidx(1)">' . $this->config['first'] . '</a>';
  98. //最后一页
  99. $the_end = '';//'<a class="end" href="javascript:voidx(' .$this->totalPages. ')">' . $this->config['last'] . '</a>';
  100. //数字连接
  101. $link_page = "";
  102. for($i = 1; $i <= $this->rollPage; $i++){
  103. if(($this->nowPage - $now_cool_page) <= 0 ){
  104. $page = $i;
  105. }elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
  106. $page = $this->totalPages - $this->rollPage + $i;
  107. }else{
  108. $page = $this->nowPage - $now_cool_page_ceil + $i;
  109. }
  110. if ($page>0) {
  111. if($page != $this->nowPage){
  112. if($page <= $this->totalPages){
  113. //$link_page .= '<a class="num" href="javascript:voidx('.$page .')">' . $page . '</a>';
  114. }else{
  115. break;
  116. }
  117. }else{
  118. $link_page .= '<span class="current">' . $page . '</span>';
  119. $pages=$page;
  120. }
  121. }
  122. }
  123. //替换分页内容
  124. $page_str = str_replace(
  125. array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
  126. array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
  127. $this->config['theme']);
  128. $total='<select name="skip" id="box-fire-page2" onchange="voidx(\'skip\')" >';
  129. for ($i=1; $i <=$this->totalPages; $i++) {
  130. if ($i==$pages)
  131. $total .='<option value="'.$i.'" selected="">'.$i.'</option>';
  132. else
  133. $total .='<option value="'.$i.'" >'.$i.'</option>';
  134. }
  135. $total .='</select>';
  136. return '<div class="page">'.$page_str.' '.$total.'</div>';
  137. }
  138. }