[PHP实例] PHP分页类

  1. <?php
  2. /**
  3. * 分页类
  4. *
  5. * author lynnluo
  6. * addtime 2011-08-15
  7. */
  8. http://m.hwjbyby.com/nxby/qtby/262.html
  9. define( 'PAGE_NAME','page' );// url '....php?page=2'中的page名称
  10. define( 'CR',"\n" );// 换行符
  11. class page
  12. {
  13. private $prev_label = ' 上一页 ';//
  14. private $next_label = ' 下一页 ';//
  15. private $first_label = ' 首页 ';//
  16. private $last_label = ' 尾页 ';//
  17. private $per_page = 10;//每页显示多少条
  18. private $adjacent_num = 2;//当前页相邻的显示量
  19. private $page_num; //总页数
  20. private $page; //第几页
  21. private $html;//返回分页的html源码
  22. private $display_str_flag = true;//首页和尾页用字符串or数字显示 true:字符串 其它则显示数字
  23. /**
  24. * 构造函数
  25. * $per_page int 每页显示多少条
  26. * $totle int 总数量
  27. */
  28. public function __construct( $per_page, $totle )
  29. {http://m.hwjbyby.com/nxby/dnlc/263.html
  30. $this->per_page = intval($per_page)<1 ? $this->per_page : intval($per_page);//每页显示条数小于1时 默认为10
  31. $this->page_num = ceil( $totle / $this->per_page );
  32. $this->page = isset($_GET[PAGE_NAME]) ? ( (intval($_GET[PAGE_NAME])>0) ? intval($_GET[PAGE_NAME]):1) : 1;//当前页码小于1时默认为第一页
  33. $this->page = $this->page > $this->page_num ? $this->page_num : $this->page;//当前页码超过最大页码时默认为最后一页
  34. $this->html = '';
  35. }
  36. /**
  37. * 私有属性赋值时的错误处理
  38. */
  39. public function __set( $aa ,$bb){}
  40. /**
  41. * 配置类的私有属性
  42. * $ary array 键值对,键为该类的私有属性
  43. */
  44. public function set( $ary = array( 'display_str_flag'=>false,
  45. 'prev_label'=>'>>',
  46. 'next_label'=>'>>',
  47. 'first_label'=>'>>',
  48. 'last_label'=>'>>',
  49. 'adjacent_num'=>5))
  50. {
  51. foreach( $ary as $key=>$value )
  52. {
  53. $this->{$key} = $value;
  54. }
  55. }
  56. http://m.hwjbyby.com/nxby/dnlc/264.html
  57. /**
  58. * 返回当前URL
  59. */
  60. private function getURL()
  61. {
  62. $url = $_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"],'?')?'':"?");
  63. $parse=parse_url($url);
  64. if(isset($parse['query']))
  65. {
  66. parse_str($parse['query'],$params);
  67. unset($params[PAGE_NAME]);
  68. $url = $parse['path'].'?'.http_build_query($params);
  69. }
  70. $url .= strpos($url, '?') ? (strpos($url, '=')?'&':'') : '' ;
  71. return $url;
  72. }
  73. /**
  74. * 首页
  75. */
  76. private function first_page()
  77. {
  78. if( $this->page == 1 )
  79. {
  80. $this->html .= '<span>' . $this->first_label . '</span>'.CR;
  81. }
  82. else
  83. {
  84. $this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=1">' . $this->first_label . '</a>'.CR;
  85. }
  86. }
  87. /**
  88. * 尾页
  89. */
  90. private function last_page()
  91. {
  92. if( $this->page == $this->page_num )
  93. {
  94. $this->html .= '<span>' . $this->last_label . '</span>'.CR;
  95. }
  96. else
  97. {
  98. $this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'='.$this->page_num.'">' . $this->last_label . '</a>'.CR;
  99. }
  100. }http://m.hwjbyby.com/nxby/lcxby/265.html
  101. /**
  102. * 上一页
  103. */
  104. private function prev_page()
  105. {
  106. if( $this->page == 1 )
  107. {
  108. $this->html .= '<span>' . $this->prev_label . '</span>'.CR;
  109. }
  110. else
  111. {
  112. $this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=' . ( $this->page - 1 ) . '">' . $this->prev_label . '</a>'.CR;
  113. }
  114. }
  115. /**
  116. * 下一页
  117. */
  118. private function next_page()
  119. {
  120. if( $this->page < $this->page_num)
  121. {
  122. $this->html .= '<a href="' . $this->getURL().PAGE_NAME.'=' . ( $this->page+1) . '">' . $this->next_label . '</a>'.CR;
  123. }
  124. else
  125. {
  126. $this->html .= '<span>' . $this->next_label . '</span>'.CR;
  127. }
  128. }
  129. /**
  130. * 第一块 第一个省略号前的那块
  131. */
  132. private function first_block()
  133. {
  134. if( $this->page > ( $this->adjacent_num+1 ) )
  135. {
  136. $this->html.= '<a href="' . $this->getURL().PAGE_NAME.'=1">1</a>'.CR;
  137. }
  138. if( $this->page > ( $this->adjacent_num+2 ) )
  139. {
  140. $this->html.= '...'.CR;
  141. }
  142. }
  143. /**
  144. * 第二块 两个省略号中间的那块
  145. */
  146. private function middle_block()
  147. {
  148. $page_min = ( $this->page > $this->adjacent_num ) ? ( $this->page - $this->adjacent_num ) : 1;
  149. $page_max = ( $this->page < ($this->page_num-$this->adjacent_num)) ? ($this->page+$this->adjacent_num) : $this->page_num ;
  150. for( $i=$page_min; $i<=$page_max; $i++)
  151. {
  152. if( $i == $this->page )
  153. {
  154. $this->html .= '<span class="current">' . $i . '</span>'.CR;
  155. }
  156. else
  157. {
  158. $this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=' . $i . '">' . $i . '</a>'.CR;
  159. }
  160. }
  161. }
  162. /**
  163. * 最后一块 最后一个省略号后的的那块
  164. */
  165. private function last_block()
  166. {http://m.hwjbyby.com/nxby/dnlc/266.html
  167. if( $this->page < ($this->page_num - $this->adjacent_num - 1))
  168. {
  169. $this->html .= '...'.CR;
  170. }
  171. if( $this->page < ($this->page_num-$this->adjacent_num ))
  172. {
  173. $this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=' . $this->page_num . '">' . $this->page_num . '</a>'.CR;
  174. }
  175. }
  176. /**
  177. * 显示分页
  178. *
  179. * $out_flag bool 输出和反回分页html的标志 true:直接输出 false:返回分页的html
  180. *
  181. */
  182. public function display( $out_flag = false )
  183. {
  184. $this->html = '<div class="pagin">'.CR;
  185. m.hwjbyby.com
  186. if( $this->display_str_flag === true )
  187. {//首页和尾页用字符串显示
  188. $this->first_page(); //显示首页
  189. $this->prev_page(); //显示上一页
  190. $this->middle_block(); //显示中间块
  191. $this->next_page(); //显示下一页
  192. $this->last_page(); //显示最后一页
  193. }
  194. else
  195. {//首页和尾页用数字显示
  196. $this->prev_page(); //显示上一页
  197. $this->first_block(); //显示第一块
  198. $this->middle_block(); //显示中间块
  199. $this->last_block(); //显示最后一块
  200. $this->next_page(); //显示下一页
  201. }
  202. $this->html .= '</div>';
  203. if($out_flag === false)
  204. {//返回分页 html码
  205. return $this->html;
  206. }
  207. else
  208. {//输出分页 html码
  209. echo $html;
  210. }
  211. }
  212. }
  213. /*************demo **************/
  214. $page = new page(3,55);
  215. $page->set( $ary = array( 'display_str_flag'=>false,
  216. 'prev_label'=>'上',
  217. 'next_label'=>'下一页',
  218. 'last_label'=>'末',));
  219. echo $page->display();
  220. /*************demo **************/
  221. ?>
  222. 分页类&nbsp;~&nbsp;6KB&nbsp;&nbsp;&nbsp;&nbsp;下载(39)
  223. ?<?php
  224. /**
  225. * 分页类
  226. *
  227. * author lynnluo
  228. * addtime 2011-08-15
  229. */
  230. define( 'PAGE_NAME','page' );// url '....php?page=2'中的page名称
  231. define( 'CR',"\n" );// 换行符
  232. class page
  233. {
  234. private $prev_label = ' 上一页 ';//
  235. private $next_label = ' 下一页 ';//
  236. private $first_label = ' 首页 ';//
  237. private $last_label = ' 尾页 ';//
  238. private $per_page = 10;//每页显示多少条
  239. private $adjacent_num = 2;//当前页相邻的显示量
  240. private $page_num; //总页数
  241. private $page; //第几页
  242. private $html;//返回分页的html源码
  243. private $display_str_flag = true;//首页和尾页用字符串or数字显示 true:字符串 其它则显示数字
  244. /**
  245. * 构造函数
  246. * $per_page int 每页显示多少条
  247. * $totle int 总数量
  248. */
  249. public function __construct( $per_page, $totle )
  250. {
  251. $this->per_page = intval($per_page)<1 ? $this->per_page : intval($per_page);//每页显示条数小于1时 默认为10
  252. $this->page_num = ceil( $totle / $this->per_page );
  253. $this->page = isset($_GET[PAGE_NAME]) ? ( (intval($_GET[PAGE_NAME])>0) ? intval($_GET[PAGE_NAME]):1) : 1;//当前页码小于1时默认为第一页
  254. $this->page = $this->page > $this->page_num ? $this->page_num : $this->page;//当前页码超过最大页码时默认为最后一页
  255. $this->html = '';
  256. }
  257. /**
  258. * 私有属性赋值时的错误处理
  259. */
  260. public function __set( $aa ,$bb){}
  261. /**
  262. * 配置类的私有属性
  263. * $ary array 键值对,键为该类的私有属性
  264. */
  265. public function set( $ary = array( 'display_str_flag'=>false,
  266. 'prev_label'=>'>>',
  267. 'next_label'=>'>>',
  268. 'first_label'=>'>>',
  269. 'last_label'=>'>>',
  270. 'adjacent_num'=>5))
  271. {
  272. foreach( $ary as $key=>$value )
  273. {
  274. $this->{$key} = $value;
  275. }
  276. }
  277. /**
  278. * 返回当前URL
  279. */
  280. private function getURL()
  281. {
  282. $url = $_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"],'?')?'':"?");
  283. $parse=parse_url($url);
  284. if(isset($parse['query']))
  285. {
  286. parse_str($parse['query'],$params);
  287. unset($params[PAGE_NAME]);
  288. $url = $parse['path'].'?'.http_build_query($params);
  289. }
  290. $url .= strpos($url, '?') ? (strpos($url, '=')?'&':'') : '' ;
  291. return $url;
  292. }
  293. /**
  294. * 首页
  295. */
  296. private function first_page()
  297. {
  298. if( $this->page == 1 )
  299. {
  300. $this->html .= '<span>' . $this->first_label . '</span>'.CR;
  301. }
  302. else
  303. {
  304. $this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=1">' . $this->first_label . '</a>'.CR;
  305. }
  306. }
  307. /**
  308. * 尾页
  309. */
  310. private function last_page()
  311. {
  312. if( $this->page == $this->page_num )
  313. {
  314. $this->html .= '<span>' . $this->last_label . '</span>'.CR;
  315. }
  316. else
  317. {
  318. $this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'='.$this->page_num.'">' . $this->last_label . '</a>'.CR;
  319. }
  320. }
  321. /**
  322. * 上一页
  323. */
  324. private function prev_page()
  325. {
  326. if( $this->page == 1 )
  327. {
  328. $this->html .= '<span>' . $this->prev_label . '</span>'.CR;
  329. }
  330. else
  331. {
  332. $this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=' . ( $this->page - 1 ) . '">' . $this->prev_label . '</a>'.CR;
  333. }
  334. }
  335. /**
  336. * 下一页
  337. */
  338. private function next_page()
  339. {
  340. if( $this->page < $this->page_num)
  341. {
  342. $this->html .= '<a href="' . $this->getURL().PAGE_NAME.'=' . ( $this->page+1) . '">' . $this->next_label . '</a>'.CR;
  343. }
  344. else
  345. {
  346. $this->html .= '<span>' . $this->next_label . '</span>'.CR;
  347. }
  348. }
  349. /**
  350. * 第一块 第一个省略号前的那块
  351. */
  352. private function first_block()
  353. {
  354. if( $this->page > ( $this->adjacent_num+1 ) )
  355. {
  356. $this->html.= '<a href="' . $this->getURL().PAGE_NAME.'=1">1</a>'.CR;
  357. }
  358. if( $this->page > ( $this->adjacent_num+2 ) )
  359. {
  360. $this->html.= '...'.CR;
  361. }
  362. }
  363. /**
  364. * 第二块 两个省略号中间的那块
  365. */
  366. private function middle_block()
  367. {
  368. $page_min = ( $this->page > $this->adjacent_num ) ? ( $this->page - $this->adjacent_num ) : 1;
  369. $page_max = ( $this->page < ($this->page_num-$this->adjacent_num)) ? ($this->page+$this->adjacent_num) : $this->page_num ;
  370. for( $i=$page_min; $i<=$page_max; $i++)
  371. {
  372. if( $i == $this->page )
  373. {
  374. $this->html .= '<span class="current">' . $i . '</span>'.CR;
  375. }
  376. else
  377. {
  378. $this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=' . $i . '">' . $i . '</a>'.CR;
  379. }
  380. }
  381. }
  382. /**
  383. * 最后一块 最后一个省略号后的的那块
  384. */
  385. private function last_block()
  386. {
  387. if( $this->page < ($this->page_num - $this->adjacent_num - 1))
  388. {
  389. $this->html .= '...'.CR;
  390. }
  391. if( $this->page < ($this->page_num-$this->adjacent_num ))
  392. {
  393. $this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=' . $this->page_num . '">' . $this->page_num . '</a>'.CR;
  394. }
  395. }
  396. /**
  397. * 显示分页
  398. *
  399. * $out_flag bool 输出和反回分页html的标志 true:直接输出 false:返回分页的html
  400. *
  401. */
  402. public function display( $out_flag = false )
  403. {
  404. $this->html = '<div class="pagin">'.CR;
  405. if( $this->display_str_flag === true )
  406. {//首页和尾页用字符串显示
  407. $this->first_page(); //显示首页
  408. $this->prev_page(); //显示上一页
  409. $this->middle_block(); //显示中间块
  410. $this->next_page(); //显示下一页
  411. $this->last_page(); //显示最后一页
  412. }
  413. else
  414. {//首页和尾页用数字显示
  415. $this->prev_page(); //显示上一页
  416. $this->first_block(); //显示第一块
  417. $this->middle_block(); //显示中间块
  418. $this->last_block(); //显示最后一块
  419. $this->next_page(); //显示下一页
  420. }
  421. $this->html .= '</div>';
  422. if($out_flag === false)
  423. {//返回分页 html码
  424. return $this->html;
  425. }
  426. else
  427. {//输出分页 html码
  428. echo $html;
  429. }
  430. }
  431. }
  432. /*************demo **************/
  433. $page = new page(3,55);
  434. $page->set( $ary = array( 'display_str_flag'=>false,
  435. 'prev_label'=>'上',
  436. 'next_label'=>'下一页',
  437. 'last_label'=>'末',));
  438. echo $page->display();
  439. /*************demo **************/
  440. ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值