Home > Backend Development > PHP Tutorial > A good php paging class code

A good php paging class code

WBOY
Release: 2016-07-25 09:05:18
Original
824 people have browsed it
  1. /**

  2. * filename: ext_page.class.php
  3. * @package:phpbean
  4. * @author :feifengxlq
  5. * @copyright :Copyright 2006 feifengxlq
  6. * @license:version 2.0
  7. * description: paging class, Four paging modes
  8. * 2.0 added functions: support custom styles, custom styles, support both PHP4 and PHP5,
  9. * to see detail, please visit http://www.phpobject.net/blog/read.php?
  10. * example:
  11. * mode four paging modes:
  12. require_once('../libs/classes/page.class.php');
  13. $page=new page(array('total'=>1000,'perpage' =>20));
  14. echo 'mode:1
    '.$page->show();
  15. echo '
    mode:2
    '.$page->show(2 );
  16. echo '
    mode:3
    '.$page->show(3);
  17. echo '
    mode:4
    '.$page->show(4 );
  18. Enable AJAX:
  19. $ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test' ));
  20. echo 'mode:1
    '.$ajaxpage->show();
  21. Adopt inherited custom paging display mode:
  22. demo:http://www.phpobject.net/blog
  23. */
  24. class page
  25. {
  26. /**
  27. * config ,public
  28. */
  29. var $page_name="PB_page";//page tag, Used to control the url page
  30. var $next_page='>';//Next page
  31. var $pre_page='<';//Previous page
  32. var $first_page='First';//Homepage
  33. var $ last_page='Last';//Last page
  34. var $pre_bar='<<';//Previous paging bar
  35. var $next_bar='>>';//Next paging bar
  36. var $format_left ='[';
  37. var $format_right=']';
  38. var $is_ajax=false;//Whether AJAX paging mode is supported

  39. /**

  40. * private
  41. *
  42. */
  43. var $pagebarnum =10;//Control the number of record strips.
  44. var $totalpage=0;//Total number of pages
  45. var $ajax_action_name='';//AJAX action name
  46. var $nowindex=1;//Current page
  47. var $url="";//url address header
  48. var $offset=0;

  49. /**

  50. * constructor构造函数
  51. *
  52. * @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']
  53. */
  54. function page($array)
  55. {
  56. if(is_array($array)){
  57. if(!array_key_exists(' total',$array))$this->error(__FUNCTION__,'need a param of total');
  58. $total=intval($array['total']);
  59. $perpage=(array_key_exists('perpage' ,$array))?intval($array['perpage']):10;
  60. $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
  61. $url=(array_key_exists('url',$array))?$array['url']:'';
  62. }else{
  63. $total=$array;
  64. $perpage=10;
  65. $nowindex='';
  66. $url='';
  67. }
  68. if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not a positive integer!') ;
  69. if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is not a positive integer!');
  70. if(!empty( $array['page_name']))$this->set('page_name',$array['page_name']);//Set pagename
  71. $this->_set_nowindex($nowindex);//Set the current page
  72. $this->_set_url($url);//Set the link address
  73. $this->totalpage=ceil($total/$perpage);
  74. $this->offset=($this->nowindex- 1)*$perpage;
  75. if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//Open AJAX mode
  76. }
  77. /**
  78. * Set the value of the specified variable name in the class. If the change does not belong to this class, an exception will be thrown
  79. *
  80. * @param string $var
  81. * @param string $value
  82. */
  83. function set($var,$value)
  84. {
  85. if(in_array($var,get_object_vars($this)))
  86. $this->$var=$value;
  87. else {
  88. $this-> ;error(__FUNCTION__,$var." does not belong to PB_Page!");
  89. }

  90. }

  91. /**
  92. * Turn on inverted AJAX mode
  93. *
  94. * @param string $action The default ajax triggered action.
  95. */
  96. function open_ajax($action)
  97. {
  98. $this->is_ajax=true;
  99. $this->ajax_action_name=$action;
  100. }
  101. /**
  102. * Get the code to display "next page"
  103. *
  104. * @param string $style
  105. * @return string
  106. */
  107. function next_page($style='')
  108. {
  109. if($this->nowindex<$this->totalpage){
  110. return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
  111. }
  112. return ''.$this->next_page.'';
  113. }

  114. /**

  115. * Get the code to display "previous page"
  116. *
  117. * @param string $style
  118. * @return string
  119. */
  120. function pre_page($style='')
  121. {
  122. if($this->nowindex>1){
  123. return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
  124. }
  125. return ''.$this->pre_page.'';
  126. }

  127. /**

  128. * Get the code to display "Home Page"
  129. *
  130. * @return string
  131. */
  132. function first_page($style='')
  133. {
  134. if($this->nowindex==1){
  135. return ''.$this->first_page.'';
  136. }
  137. return $this->_get_link($this->_get_url(1),$this->first_page,$style);
  138. }

  139. /**

  140. * Get the code to display the "last page"
  141. *
  142. * @return string
  143. */
  144. function last_page($style='')
  145. {
  146. if($this->nowindex==$this->totalpage){
  147. return ''.$this->last_page.'';
  148. }
  149. return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
  150. }

  151. function nowbar($style='',$nowindex_style='')

  152. {
  153. $plus=ceil($this->pagebarnum/2);
  154. if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
  155. $begin=$this->nowindex-$plus+1;
  156. $begin=($begin>=1)?$begin:1;
  157. $return='';
  158. for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
  159. {
  160. if($i<=$this->totalpage){
  161. if($i!=$this->nowindex)
  162. $return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));
  163. else
  164. $return.=$this->_get_text(''.$i.'');
  165. }else{
  166. break;
  167. }
  168. $return.="n";
  169. }
  170. unset($begin);
  171. return $return;
  172. }
  173. /**
  174. * Get the code to display the jump button
  175. *
  176. * @return string
  177. */
  178. function select()
  179. {
  180. $return='';
  181. return $return;
  182. }

  183. /**

  184. * Get the value required for limit in the mysql statement
  185. *
  186. * @return string
  187. */
  188. function offset()
  189. {
  190. return $this->offset;
  191. }

  192. /**

  193. * Control paging display style
  194. *
  195. * @param int $mode
  196. * @return string
  197. */
  198. function show($mode=1)
  199. {
  200. switch ($mode)
  201. {
  202. case '1':
  203. $this->next_page='下一页';
  204. $this->pre_page='上一页';
  205. return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
  206. break;
  207. case '2':
  208. $this->next_page='下一页';
  209. $this->pre_page='上一页';
  210. $this->first_page='首页';
  211. $this->last_page='尾页';
  212. return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
  213. break;
  214. case '3':
  215. $this->next_page='下一页';
  216. $this->pre_page='上一页';
  217. $this->first_page='首页';
  218. $this->last_page='尾页';
  219. return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
  220. break;
  221. case '4':
  222. $this->next_page='下一页';
  223. $this->pre_page='上一页';
  224. return $this->pre_page().$this->nowbar().$this->next_page();
  225. break;
  226. case '5':
  227. return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
  228. break;
  229. }

  230. }

  231. /*------private function (私有方法)---------*/
  232. /**@link: http://bbs.it-home.org
  233. * Set url header address
  234. * @param: String $url
  235. * @return boolean
  236. */
  237. function _set_url($url="")
  238. {
  239. if(!empty($url)){
  240. //手动设置
  241. $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
  242. }else{
  243. //自动获取
  244. if(empty($_SERVER['QUERY_STRING'])){
  245. //不存在QUERY_STRING时
  246. $this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
  247. }else{
  248. //
  249. if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){
  250. //地址存在页面参数
  251. $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER['REQUEST_URI']);
  252. $last=$this->url[strlen($this->url)-1];
  253. if($last=='?'||$last=='&'){
  254. $this->url.=$this->page_name."=";
  255. }else{
  256. $this->url.='&'.$this->page_name."=";
  257. }
  258. }else{
  259. //
  260. $this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
  261. }//end if
  262. }//end if
  263. }//end if
  264. }

  265. /**

  266. * Set current page
  267. *
  268. */
  269. function _set_nowindex($nowindex)
  270. {
  271. if(empty($nowindex)){
  272. //系统获取

  273. if(isset($_GET[$this->page_name])){

  274. $this->nowindex=intval($_GET[$this->page_name]);
  275. }
  276. }else{
  277. //手动设置
  278. $this->nowindex=intval($nowindex);
  279. }
  280. }

  281. /**

  282. * Return the address value for the specified page
  283. *
  284. * @param int $pageno
  285. * @return string $url
  286. */
  287. function _get_url($pageno=1)
  288. {
  289. return $this->url.$pageno;
  290. }

  291. /**

  292. * Get pagination display text, for example, by default _get_text('1') will return [1]
  293. *
  294. * @param String $str
  295. * @return string $url
  296. */
  297. function _get_text($str)
  298. {
  299. return $this->format_left.$str.$this->format_right;
  300. }

  301. /**

  302. * Get link address
  303. */
  304. function _get_link($url,$text,$style=''){
  305. $style=(empty($style))?'':'class="'.$style.'"';
  306. if($this->is_ajax){
  307. //如果是使用AJAX模式
  308. return ''.$text.'';
  309. }else{
  310. return ''.$text.'';
  311. }
  312. }
  313. /**
  314. * Error handling methods
  315. */
  316. function error($function,$errormsg)
  317. {
  318. die('Error in file '.__FILE__.' ,Function '.$function.'() :'.$errormsg);
  319. }
  320. }
  321. $page=new page(array('total'=>1000,'perpage'=>20));
  322. echo 'mode:1
    '.$page->show();
  323. echo '
    mode:2
    '.$page->show(2);
  324. echo '
    mode:3
    '.$page->show(3);
  325. echo '
    mode:4
    '.$page->show(4);
  326. echo '
    开始AJAX模式:';
  327. $ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
  328. echo 'mode:1
    '.$ajaxpage->show();
  329. ?>

复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template