Home >Backend Development >PHP Tutorial >PHP paging class calling example tutorial

PHP paging class calling example tutorial

WBOY
WBOYOriginal
2016-07-25 08:52:36801browse
  1. /**

  2. * filename: ext_page.class.php
  3. * @package:phpbean
  4. * description: Super powerful paging class, four paging modes, and the default paging style is similar to Baidu and Google.
  5. * 2.0 added functions: support custom styles, custom styles, support both PHP4 and PHP5,
  6. * example:
  7. * Four paging modes:
  8. require_once('../libs/classes/page.class.php' );
  9. $page=new page(array('total'=>1000,'perpage'=>20));
  10. echo 'mode:1
    '.$page->show();
  11. echo '
    mode:2
    '.$page->show(2);
  12. echo '
    mode:3
    '.$page->show(3);
  13. echo '
    mode:4
    '.$page->show(4);
  14. Turn on AJAX: bbs.it-home.org
  15. $ajaxpage=new page(array('total'=> 1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
  16. echo 'mode:1
    '.$ajaxpage->show( );
  17. Adopt inherited custom paging display mode:
  18. */
  19. class Page
  20. {
  21. /**
  22. * config ,public
  23. */
  24. var $page_name="p";//page tag , used to control the url page. For example, PB_page in xxx.php?PB_page=2
  25. var $next_page='>';//Next page
  26. var $pre_page='<';//Previous page
  27. var $first_page='First' ;//Homepage
  28. var $last_page='Last';//Last page
  29. var $pre_bar='<<';//Previous paging bar
  30. var $next_bar='>>';//Next A paging bar
  31. var $format_left='';
  32. var $format_right='';
  33. var $is_ajax=false;//Whether AJAX paging mode is supported

  34. /**

  35. * private
  36. *
  37. * /
  38. var $pagebarnum=10;//Control the number of record bars.
  39. var $totalpage=0;//Total number of pages
  40. var $ajax_action_name='';//AJAX action name
  41. var $nowindex=1;//Current page
  42. var $url="";//url address header
  43. var $offset=0;

  44. /**

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

  85. }

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

  109. /**

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

  122. /**

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

  134. /**

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

  146. function nowbar($style='',$nowindex_style='c')

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

  178. /**

  179. * Get the value required for limit in the mysql statement
  180. *
  181. * @return string
  182. */
  183. function offset()
  184. {
  185. return $this->offset;
  186. }

  187. /**

  188. * 控制分页显示风格(你可以增加相应的风格)
  189. *
  190. * @param int $mode
  191. * @return string
  192. */
  193. function show($mode=1)
  194. {
  195. switch ($mode)
  196. {
  197. case '1':
  198. $this->next_page='下一页';
  199. $this->pre_page='上一页';
  200. return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
  201. break;
  202. case '2':
  203. $this->next_page='下一页';
  204. $this->pre_page='上一页';
  205. $this->first_page='首页';
  206. $this->last_page='尾页';
  207. return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
  208. break;
  209. case '3':
  210. $this->next_page='下一页';
  211. $this->pre_page='上一页';
  212. $this->first_page='首页';
  213. $this->last_page='尾页';
  214. return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
  215. break;
  216. case '4':
  217. $this->next_page='>';
  218. $this->pre_page='<';
  219. $this->first_page='<<';
  220. $this->last_page='>>';
  221. return $this->first_page().$this->pre_page().$this->nowbar().$this->next_page().$this->last_page();
  222. break;
  223. case '5':
  224. return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
  225. break;
  226. }

  227. }

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

  262. /**

  263. * 设置当前页面
  264. *
  265. */
  266. function _set_nowindex($nowindex)
  267. {
  268. if(empty($nowindex)){
  269. //系统获取

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

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

  278. /**

  279. * 为指定的页面返回地址值
  280. *
  281. * @param int $pageno
  282. * @return string $url
  283. */
  284. function _get_url($pageno=1)
  285. {
  286. return $this->url.$pageno . '.html';
  287. }

  288. /**

  289. * 获取分页显示文字,比如说默认情况下_get_text('1')将返回[1]
  290. *
  291. * @param String $str
  292. * @return string $url
  293. */
  294. function _get_text($str)
  295. {
  296. return $this->format_left.$str.$this->format_right;
  297. }

  298. /**

  299. * 获取链接地址
  300. */
  301. function _get_link($url,$text,$style=''){
  302. $style=(empty($style))?'':'class="'.$style.'"';
  303. if($this->is_ajax){
  304. //如果是使用AJAX模式
  305. return ''.$text.'';
  306. }else{
  307. return ''.$text.'';
  308. }
  309. }
  310. /**
  311. * 出错处理方式
  312. */
  313. function error($function,$errormsg)
  314. {
  315. die('Error in file '.__FILE__.' ,Function '.$function.'() :'.$errormsg);
  316. }
  317. }
  318. ?>

复制代码


Statement:
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