Page.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. namespace Think;
  12. /**
  13. * 分页类
  14. */
  15. class Page
  16. {
  17. public $firstRow; // 起始行数
  18. public $listRows; // 列表每页显示行数
  19. public $parameter; // 分页跳转时要带的参数
  20. public $totalRows; // 总行数
  21. public $totalPages; // 分页总页面数
  22. public $rollPage = 11; // 分页栏每页显示的页数
  23. private $p = 'p'; //分页参数名
  24. private $url = ''; //当前链接URL
  25. private $nowPage = 1;
  26. // 分页显示定制
  27. private $config = array(
  28. 'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
  29. 'prev' => '上一页',
  30. 'next' => '下一页',
  31. 'first' => '1...',
  32. 'last' => '...%TOTAL_PAGE%',
  33. 'theme' => '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%',
  34. );
  35. /**
  36. * 架构函数
  37. * @param array $totalRows 总的记录数
  38. * @param array $listRows 每页显示记录数
  39. * @param array $parameter 分页跳转的参数
  40. */
  41. public function __construct($totalRows, $listRows = 20, $parameter = array())
  42. {
  43. C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称
  44. /* 基础设置 */
  45. $this->totalRows = $totalRows; //设置总记录数
  46. $this->listRows = $listRows; //设置每页显示行数
  47. $this->parameter = empty($parameter) ? $_GET : $parameter;
  48. $this->nowPage = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
  49. $this->nowPage = $this->nowPage > 0 ? $this->nowPage : 1;
  50. $this->firstRow = $this->listRows * ($this->nowPage - 1);
  51. if ($this->isWap()) {
  52. $this->rollPage = 3;
  53. $this->config['theme'] = '%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%';
  54. }
  55. }
  56. /**
  57. * 定制分页链接设置
  58. * @param string $name 设置名称
  59. * @param string $value 设置值
  60. */
  61. public function setConfig($name, $value)
  62. {
  63. if (isset($this->config[$name])) {
  64. $this->config[$name] = $value;
  65. }
  66. }
  67. /**
  68. * 生成链接URL
  69. * @param integer $page 页码
  70. * @return string
  71. */
  72. private function url($page)
  73. {
  74. return str_replace(urlencode('[PAGE]'), $page, $this->url);
  75. }
  76. /**
  77. * 组装分页链接
  78. * @return string
  79. */
  80. public function show()
  81. {
  82. if (0 == $this->totalRows) {
  83. return '';
  84. }
  85. /* 生成URL */
  86. $this->parameter[$this->p] = '[PAGE]';
  87. $this->url = U(ACTION_NAME, $this->parameter);
  88. /* 计算分页信息 */
  89. $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数
  90. if (!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
  91. $this->nowPage = $this->totalPages;
  92. }
  93. /* 计算分页临时变量 */
  94. $now_cool_page = $this->rollPage / 2;
  95. $now_cool_page_ceil = ceil($now_cool_page);
  96. //上一页
  97. $up_row = $this->nowPage - 1;
  98. $up_page = $up_row > 0 ? '<li><a class="prev" href="' . $this->url($up_row) . '">' . $this->config['prev'] . '</a></li>' : '';
  99. //下一页
  100. $down_row = $this->nowPage + 1;
  101. $down_page = ($down_row <= $this->totalPages) ? '<li><a class="next" href="' . $this->url($down_row) . '">' . $this->config['next'] . '</a></li>' : '';
  102. //第一页
  103. $the_first = '';
  104. if ($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1) {
  105. $the_first = '<li><a class="first" href="' . $this->url(1) . '">' . $this->config['first'] . '</a></li>';
  106. }
  107. //最后一页
  108. $the_end = '';
  109. if ($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages) {
  110. $the_end = '<li><a class="end" href="' . $this->url($this->totalPages) . '">' . $this->config['last'] . '</a></li>';
  111. }
  112. //数字连接
  113. $link_page = "";
  114. for ($i = 1; $i <= $this->rollPage; $i++) {
  115. if (($this->nowPage - $now_cool_page) <= 0) {
  116. $page = $i;
  117. } elseif (($this->nowPage + $now_cool_page - 1) >= $this->totalPages) {
  118. $page = $this->totalPages - $this->rollPage + $i;
  119. } else {
  120. $page = $this->nowPage - $now_cool_page_ceil + $i;
  121. }
  122. if ($page > 0 && $page != $this->nowPage) {
  123. if ($page <= $this->totalPages) {
  124. $link_page .= '<li><a class="num" href="' . $this->url($page) . '">' . $page . '</a></li>';
  125. } else {
  126. break;
  127. }
  128. } else {
  129. if ($page > 0 && 1 != $this->totalPages) {
  130. $link_page .= '<li class="active current"><span class="num">' . $page . '</span></li>';
  131. }
  132. }
  133. }
  134. if ($link_page) {
  135. if (!$up_page) {
  136. $up_page = '<li><a class="prev" href="#">没有了</a></li>';
  137. }
  138. if (!$down_page) {
  139. $down_page = '<li><a class="next" href="#">没有了</a></li>';
  140. }
  141. }
  142. //替换分页内容
  143. $page_str = str_replace(
  144. array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
  145. array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
  146. $this->config['theme']);
  147. return "{$page_str}";
  148. }
  149. /**
  150. * 检测是否使用手机访问
  151. * @access public
  152. * @return bool
  153. */
  154. public function isWap()
  155. {
  156. if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
  157. return true;
  158. } elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) {
  159. return true;
  160. } elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
  161. return true;
  162. } elseif (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])) {
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. }
  168. }