一个实例php mysql模板分页类

  1. <?php
  2. /*
  3. * 模板分页类,源于easp的数据库分页方法,算是easp分页的的php独立版
  4. * 支持动态和静态分页方式
  5. * easp官网http://easp.lengshi.com/
  6. * 作者:钟晶晶
  7. * 日期:2010-11-3
  8. * 邮箱:zhongjingjing@gmail.com
  9. * 博客:http://blog.zaimer.com
  10. * page([总记录数=1],[分页大小=20],[当前页=1],[显示页数=6],[分页参数='page'],[分页链接=当前页面],[是否静态=false])
  11. * 动态:
  12. * 简单用法:
  13. * $page = new page(50);
  14. * $page->setpager('zjj','<div class="newpager">共有{recordcount} 个商品&nbsp;&nbsp;当前第&nbsp;{pageindex}&nbsp;页&nbsp;/&nbsp;共&nbsp;{pagecount}&nbsp;页&nbsp;分页:&nbsp;{first}{prev}&nbsp;&nbsp;{list}&nbsp;&nbsp;{next}{last}&nbsp;&nbsp;转到&nbsp;{jump}&nbsp;页</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
  15. * echo $page->getpager('zjj');
  16. * 全参数用法:
  17. * $page = new page(50,20,1,6,'page','prrr.php',false);
  18. * $page->setpager('zjj','<div class="newpager">共有{recordcount} 个商品&nbsp;&nbsp;当前第&nbsp;{pageindex}&nbsp;页&nbsp;/&nbsp;共&nbsp;{pagecount}&nbsp;页&nbsp;分页:&nbsp;{first}{prev}&nbsp;&nbsp;{list}&nbsp;&nbsp;{next}{last}&nbsp;&nbsp;转到&nbsp;{jump}&nbsp;页</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
  19. * echo $page->getpager('zjj');
  20. * 静态:
  21. * $page = new page(50,20,1,6,'page','prrr{page}.html',true);
  22. * $page->setpager('zjj','<div class="newpager">共有{recordcount} 个商品&nbsp;&nbsp;当前第&nbsp;{pageindex}&nbsp;页&nbsp;/&nbsp;共&nbsp;{pagecount}&nbsp;页&nbsp;分页:&nbsp;{first}{prev}&nbsp;&nbsp;{list}&nbsp;&nbsp;{next}{last}&nbsp;&nbsp;转到&nbsp;{jump}&nbsp;页</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
  23. * echo $page->getpager('zjj');
  24. */
  25. class page {
  26. private $page_size; //每页显示的条目数
  27. private $total_size; //总条目数
  28. private $current_page; //当前被选中的页
  29. private $sub_pages; //每次显示的页数
  30. private $total_pages; //总页数
  31. private $page_tpl = array (); // 分页模板
  32. private $pageparam;
  33. private $pagelink;
  34. private $static;
  35. function __construct($total_size = 1, $page_size = 20, $current_page = 1, $sub_pages = 6, $pageparam = 'page', $pagelink = '', $static = false) {
  36. $this->page_size = intval ( $page_size );
  37. $this->total_size = intval ( $total_size );
  38. if (! $current_page) {
  39. $this->current_page = 1;
  40. } else {
  41. $this->current_page = intval ( $current_page );
  42. }
  43. $this->total_pages = ceil ( $total_size / $page_size );
  44. $this->sub_pages = intval ( $sub_pages );
  45. $this->pageparam = $pageparam;
  46. $this->pagelink = (emptyempty ( $pagelink ) ? $_server ["php_self"] : $pagelink);
  47. $this->static = $static;
  48. $this->page_tpl ['default'] = array ('tpl' => '<div class="pager">{first}{prev}{liststart}{list}{listend}{next}{last} 跳转到{jump}页</div>', 'config' => array () );
  49. }
  50. public function __set($param, $value) {
  51. $this->$param = $value;
  52. }
  53. public function __get($param) {
  54. return $this->$param;
  55. }
  56. /*
  57. __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
  58. */
  59. function __destruct() {
  60. unset ( $page_size ); //每页显示的条目数
  61. unset ( $total_size ); //总条目数
  62. unset ( $current_page ); //当前被选中的页
  63. unset ( $sub_pages ); //每次显示的页数
  64. unset ( $total_pages ); //总页数
  65. unset ( $page_tpl ); // 分页模板
  66. unset ( $pageparam ); //分页参数,默认page
  67. unset ( $pagelink );
  68. unset ( $static );
  69. }
  70. private function urlparameters($url = array()) {
  71. foreach ( $url as $key => $val ) {
  72. if ($key != $this->pageparam)
  73. $arg [] = $key . '=' . $val;
  74. }
  75. $arg [] = $this->pageparam . '=*';
  76. if ($this->static)
  77. return str_replace ( '{page}', '*', $this->pagelink );
  78. else
  79. return $this->pagelink . '?' . implode ( '&', $arg );
  80. }
  81. public function setpager($tpl_name = 'default', $tpl = '', $config = array()) {
  82. if (emptyempty ( $tpl ))
  83. $tpl = $this->page_tpl ['default'] ['tpl'];
  84. if (emptyempty ( $config ))
  85. $config = $this->page_tpl ['default'] ['config'];
  86. $this->page_tpl [$tpl_name] = array ('tpl' => $tpl, 'config' => $config );
  87. }
  88. public function getpager($tpl_name = 'default') {
  89. $this->getcurrentpage ();
  90. return $this->pager ( $this->page_tpl [$tpl_name] );
  91. }
  92. public function getcurrentpage() {
  93. $this->current_page = ($_get [$this->pageparam] <= intval ( $this->total_pages ) ? ($_get [$this->pageparam] < 1 ? 1 : $_get [$this->pageparam]) : intval ( $this->total_pages ));
  94. }
  95. public function pager($page_tpl = '') {
  96. if (emptyempty ( $page_tpl ))
  97. $page_tpl = $this->page_tpl ['default'];
  98. $cfg = array ('recordcount' => intval ( $this->total_size ), 'pageindex' => intval ( $this->current_page ), 'pagecount' => intval ( $this->total_pages ), 'pagesize' => intval ( $this->page_size ), 'listlong' => intval ( $this->sub_pages ), 'listsidelong' => 2, 'list' => '*', 'currentclass' => 'current', 'link' => $this->urlparameters ( $_get ), 'first' => '&laquo;', 'prev' => '&#8249;', 'next' => '&#8250;', 'last' => '&raquo;', 'more' => '...', 'disabledclass' => 'disabled', 'jump' => 'input', 'jumpplus' => '', 'jumpaction' => '', 'jumplong' => 50 );
  99. if (! emptyempty ( $page_tpl ['config'] )) {
  100. foreach ( $page_tpl ['config'] as $key => $val ) {
  101. if (array_key_exists ( $key, $cfg ))
  102. $cfg [$key] = $val;
  103. }
  104. }
  105. $tmpstr = $page_tpl ['tpl'];
  106. $pstart = $cfg ['pageindex'] - (($cfg ['listlong'] / 2) + ($cfg ['listlong'] % 2)) + 1;
  107. $pend = $cfg ['pageindex'] + $cfg ['listlong'] / 2;
  108. if ($pstart < 1) {
  109. $pstart = 1;
  110. $pend = $cfg ['listlong'];
  111. }
  112. if ($pend > $cfg ['pagecount']) {
  113. $pstart = $cfg ['pagecount'] - $cfg ['listlong'] + 1;
  114. $pend = $cfg ['pagecount'];
  115. }
  116. if ($pstart < 1)
  117. $pstart = 1;
  118. for($i = $pstart; $i <= $pend; $i ++) {
  119. if ($i == $cfg ['pageindex'])
  120. $plist .= '<span class="' . $cfg ['currentclass'] . '" >' . str_replace ( '*', $i, $cfg ['list'] ) . '</span> ';
  121. else
  122. $plist .= ' <a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '"> ' . str_replace ( '*', $i, $cfg ['list'] ) . '</a> ';
  123. }
  124. if ($cfg ['listsidelong'] > 0) {
  125. if ($cfg ['listsidelong'] < $pstart) {
  126. for($i = 1; $i <= $cfg ['listsidelong']; $i ++) {
  127. $pliststart .= '<a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '">' . str_replace ( '*', $i, $cfg ['list'] ) . '</a> ';
  128. }
  129. $pliststart .= ($cfg ['listsidelong'] + 1) == $pstart ? '' : $cfg ['more'] . ' ';
  130. } else {
  131. if ($cfg ['listsidelong'] >= $pstart && $pstart > 1) {
  132. for($i = 1; $i <= ($pstart - 1); $i ++) {
  133. $pliststart .= '<a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '">' . str_replace ( '*', $i, $cfg ['list'] ) . '</a> ';
  134. }
  135. }
  136. }
  137. if (($cfg ['pagecount'] - $cfg ['listsidelong']) > $pend) {
  138. $plistend = ' ' . $cfg ['more'] . $plistend;
  139. for($i = (($cfg ['pagecount'] - $cfg ['listsidelong']) + 1); $i <= $cfg ['pagecount']; $i ++) {
  140. $plistend .= ' <a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '"> ' . str_replace ( '*', $i, $cfg ['list'] ) . ' </a> ';
  141. }
  142. } else {
  143. if (($cfg ['pagecount'] - $cfg ['listsidelong']) <= $pend && $pend < $cfg ['pagecount']) {
  144. for($i = ($pend + 1); $i <= $cfg ['pagecount']; $i ++) {
  145. $plistend .= ' <a href="' . str_replace ( '*', $i, $cfg ['link'] ) . '"> ' . str_replace ( '*', $i, $cfg ['list'] ) . ' </a> ';
  146. }
  147. }
  148. }
  149. }
  150. if ($cfg ['pageindex'] > 1) {
  151. $pfirst = ' <a href="' . str_replace ( '*', '1', $cfg ['link'] ) . '">' . $cfg ['first'] . '</a> ';
  152. $pprev = ' <a href="' . str_replace ( '*', $cfg ['pageindex'] - 1, $cfg ['link'] ) . '">' . $cfg ['prev'] . '</a> ';
  153. } else {
  154. $pfirst = ' <span class="' . $cfg ['disabledclass'] . '">' . $cfg ['first'] . '</span> ';
  155. $pprev = ' <span class="' . $cfg ['disabledclass'] . '">' . $cfg ['prev'] . '</span> ';
  156. }
  157. if ($cfg ['pageindex'] < $cfg ['pagecount']) {
  158. $plast = ' <a href="' . str_replace ( '*', $cfg ['pagecount'], $cfg ['link'] ) . '">' . $cfg ['last'] . '</a> ';
  159. $pnext = ' <a href="' . str_replace ( '*', $cfg ['pageindex'] + 1, $cfg ['link'] ) . '">' . $cfg ['next'] . '</a> ';
  160. } else {
  161. $plast = ' <span class="' . $cfg ['disabledclass'] . '">' . $cfg ['last'] . '</span> ';
  162. $pnext = ' <span class="' . $cfg ['disabledclass'] . '">' . $cfg ['next'] . '</span> ';
  163. }
  164. switch (strtolower ( $cfg ['jump'] )) {
  165. case 'input' :
  166. $pjumpvalue = 'this.value';
  167. $pjump = '<input type="text" size="3" title="请输入要跳转到的页数并回车"' . (($cfg ['jumpplus'] == '') ? '' : ' ' . $cfg ['jumpplus']);
  168. $pjump .= ' onkeydown=":if(event.charcode==13||event.keycode==13){if(!isnan(' . $pjumpvalue . ')){';
  169. $pjump .= ($cfg ['jumpaction'] == '' ? ((strtolower ( substr ( $cfg ['link'], 0, 11 ) ) == 'javascript:') ? str_replace ( '*', $pjumpvalue, substr ( $cfg ['link'], 12 ) ) : " document.location.href='" . str_replace ( '*', ''+' . $pjumpvalue . '+'', $cfg ['link'] ) . '';') : str_replace ( "*", $pjumpvalue, $cfg ['jumpaction'] ));
  170. $pjump .= '}return false;}" />';
  171. break;
  172. case 'select' :
  173. $pjumpvalue = "this.options[this.selectedindex].value";
  174. $pjump = '<select ' . ($cfg ['jumpplus'] == '' ? ' ' . $cfg ['jumpplus'] . ' onchange="javascript:' : $cfg ['jumpplus']);
  175. $pjump .= ($cfg ['jumpaction'] == '' ? ((strtolower ( substr ( $cfg ['link'], 0, 11 ) ) == 'javascript:') ? str_replace ( '*', $pjumpvalue, substr ( $cfg ['link'], 12 ) ) : " document.location.href='" . str_replace ( '*', ''+' . $pjumpvalue . '+'', $cfg ['link'] ) . '';') : str_replace ( "*", $pjumpvalue, $cfg ['jumpaction'] ));
  176. $pjump .= '" title="请选择要跳转到的页数"> ';
  177. if ($cfg ['jumplong'] == 0) {
  178. for($i = 0; $i <= $cfg ['pagecount']; $i ++) {
  179. $pjump .= '<option value="' . $i . '"' . (($i == $cfg ['pageindex']) ? ' selected="selected"' : '') . ' >' . $i . '</option> ';
  180. }
  181. } else {
  182. $pjumplong = intval ( $cfg ['jumplong'] / 2 );
  183. $pjumpstart = ((($cfg ['pageindex'] - $pjumplong) < 1) ? 1 : ($cfg ['pageindex'] - $pjumplong));
  184. $pjumpstart = ((($cfg ['pagecount'] - $cfg ['pageindex']) < $pjumplong) ? ($pjumpstart - ($pjumplong - ($cfg ['pagecount'] - $cfg ['pageindex'])) + 1) : $pjumpstart);
  185. $pjumpstart = (($pjumpstart < 1) ? 1 : $pjumpstart);
  186. $j = 1;
  187. for($i = $pjumpstart; $i <= $cfg ['pageindex']; $i ++, $j ++) {
  188. $pjump .= '<option value="' . $i . '"' . (($i == $cfg ['pageindex']) ? ' selected="selected"' : '') . '>' . $i . '</option> ';
  189. }
  190. $pjumplong = $cfg ['pagecount'] - $cfg ['pageindex'] < $pjumplong ? $pjumplong : $pjumplong + ($pjumplong - $j) + 1;
  191. $pjumpend = $cfg ['pageindex'] + $pjumplong > $cfg ['pagecount'] ? $cfg ['pagecount'] : $cfg ['pageindex'] + $pjumplong;
  192. for($i = $cfg ['pageindex'] + 1; $i <= $pjumpend; $i ++) {
  193. $pjump .= '<option value="' . $i . '">' . $i . '</option> ';
  194. }
  195. }
  196. $pjump .= '</select>';
  197. break;
  198. }
  199. $patterns = array ('/{recordcount}/', '/{pagecount}/', '/{pageindex}/', '/{pagesize}/', '/{list}/', '/{liststart}/', '/{listend}/', '/{first}/', '/{prev}/', '/{next}/', '/{last}/', '/{jump}/' );
  200. $replace = array ($cfg ['recordcount'], $cfg ['pagecount'], $cfg ['pageindex'], $cfg ['pagesize'], $plist, $pliststart, $plistend, $pfirst, $pprev, $pnext, $plast, $pjump );
  201. $tmpstr = chr ( 13 ) . chr ( 10 ) . preg_replace ( $patterns, $replace, $tmpstr ) . chr ( 13 ) . chr ( 10 );
  202. unset ( $cfg );
  203. return $tmpstr;
  204. }
  205. }
  206. ?>