Home  >  Article  >  Backend Development  >  Share: An example code of PHP page turning (paging) class

Share: An example code of PHP page turning (paging) class

WBOY
WBOYOriginal
2016-07-25 08:52:25925browse
  1. /**
  2. * filename: ext_page.class.php
  3. * @package:phpbean
  4. * descrīption: Super powerful paging class, four paging modes, the default paging style is similar to Baidu and Google.
  5. * 2.0 added functions: supports custom styles, custom styles, supports 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:
  15. $ajaxpage=new page(array('total'=>1000,'perpage'=> ;20,'ajax'=>'ajax_page','page_name'=>'test'));
  16. echo 'mode:1
    '.$ajaxpage->show();
  17. Use inheritance customization Pagination display mode.
  18. Editing: Scripting School http://bbs.it-home.org
  19. */
  20. class _page
  21. {
  22. /**
  23. * config ,public
  24. */
  25. var $page_name="PB_page";//page tag, used to control url Page. For example, PB_page in xxx.php?PB_page=2
  26. var $next_page='>';//Next page
  27. var $pre_page='<';//Previous page
  28. var $first_page='First' ;//Homepage
  29. var $last_page='Last';//Last page
  30. var $pre_bar='<<';//Previous paging bar
  31. var $next_bar='>>';//Next A paging bar
  32. var $format_left='[';
  33. var $format_right=']';
  34. var $is_ajax=false;//Whether AJAX paging mode is supported
  35. /**
  36. * private
  37. *
  38. */
  39. var $pagebarnum= 10;//Control the number of record strips.
  40. var $totalpage=0;//Total number of pages
  41. var $ajax_action_name='';//AJAX action name
  42. var $nowindex=1;//Current page
  43. var $url="";//url address header
  44. var $offset=0;
  45. /**
  46. * constructor构造函数
  47. *
  48. * @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']...
  49. */
  50. function page($array)
  51. {
  52. if(is_array($array)){
  53. if(!array_key_exists('total',$array))$ this->error(__FUNCTION__,'need a param of total');
  54. $total=intval($array['total']);
  55. $perpage=(array_key_exists('perpage',$array))?intval( $array['perpage']):10;
  56. $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
  57. $url=(array_key_exists('url ',$array))?$array['url']:'';
  58. }else{
  59. $total=$array;
  60. $perpage=10;
  61. $nowindex='';
  62. $url='';
  63. }
  64. if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not a positive integer!');
  65. if((!is_int( $perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is not a positive integer!');
  66. if(!empty($array['page_name']) )$this->set('page_name',$array['page_name']);//Set pagename
  67. $this->_set_nowindex($nowindex);//Set the current page
  68. $this->_set_url( $url);//Set the link address
  69. $this->totalpage=ceil($total/$perpage);
  70. $this->offset=($this->nowindex-1)*$perpage;
  71. if (!empty($array['ajax']))$this->open_ajax($array['ajax']);//Open AJAX mode
  72. }
  73. /**
  74. * 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
  75. *
  76. * @param string $var
  77. * @param string $value
  78. */
  79. function set($ var,$value)
  80. {
  81. if(in_array($var,get_object_vars($this)))
  82. $this->$var=$value;
  83. else {
  84. $this->error(__FUNCTION__,$var. " does not belong to PB_Page!");
  85. }
  86. }
  87. /**
  88. * Turn on reverse AJAX mode
  89. *
  90. * @param string $action Default ajax triggered action.
  91. */
  92. function open_ajax($action)
  93. {
  94. $this->is_ajax=true;
  95. $this->ajax_action_name=$action;
  96. }
  97. /**
  98. * Get the code to display "next page"
  99. *
  100. * @param string $style
  101. * @return string
  102. */
  103. function next_page($style='')
  104. {
  105. if($this->nowindex<$this->totalpage){
  106. return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
  107. }
  108. return ''.$this->next_page.'';
  109. }
  110. /**
  111. * Get the code to display "previous page"
  112. *
  113. * @param string $style
  114. * @return string
  115. */
  116. function pre_page($style='')
  117. {
  118. if($this->nowindex>1){
  119. return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
  120. }
  121. return ''.$this->pre_page.'';
  122. }
  123. /**
  124. * Get the code to display "Home Page"
  125. *
  126. * @return string
  127. */
  128. function first_page($style='')
  129. {
  130. if($this->nowindex==1){
  131. return ''.$this->first_page.'';
  132. }
  133. return $this->_get_link($this->_get_url(1),$this->first_page,$style);
  134. }
  135. /**
  136. * Get the code to display the "last page"
  137. *
  138. * @return string
  139. */
  140. function last_page($style='')
  141. {
  142. if($this->nowindex==$this->totalpage){
  143. return ''.$this->last_page.'';
  144. }
  145. return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
  146. }
  147. function nowbar($style='',$nowindex_style='')
  148. {
  149. $plus=ceil($this->pagebarnum/2);
  150. if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
  151. $begin=$this->nowindex-$plus+1;
  152. $begin=($begin>=1)?$begin:1;
  153. $return='';
  154. for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
  155. {
  156. if($i<=$this->totalpage){
  157. if($i!=$this->nowindex)
  158. $return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));
  159. else
  160. $return.=$this->_get_text(''.$i.'');
  161. }else{
  162. break;
  163. }
  164. $return.="n";
  165. }
  166. unset($begin);
  167. return $return;
  168. }
  169. /**
  170. * Get the code to display the jump button
  171. *
  172. * @return string
  173. */
  174. function select()
  175. {
  176. $return='';
  177. return $return;
  178. }
  179. /**
  180. * Get the value required for limit in the mysql statement
  181. *
  182. * @return string
  183. */
  184. function offset()
  185. {
  186. return $this->offset;
  187. }
  188. /**
  189. * Control paging display style (you can add corresponding style)
  190. *
  191. * @param int $mode
  192. * @return string
  193. */
  194. function show($mode=1)
  195. {
  196. switch ($mode)
  197. {
  198. case '1':
  199. $this->next_page='下一页';
  200. $this->pre_page='上一页';
  201. return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
  202. break;
  203. case '2':
  204. $this->next_page='下一页';
  205. $this->pre_page='上一页';
  206. $this->first_page='首页';
  207. $this->last_page='尾页';
  208. return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
  209. break;
  210. case '3':
  211. $this->next_page='下一页';
  212. $this->pre_page='上一页';
  213. $this->first_page='首页';
  214. $this->last_page='尾页';
  215. return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
  216. break;
  217. case '4':
  218. $this->next_page='下一页';
  219. $this->pre_page='上一页';
  220. return $this->pre_page().$this->nowbar().$this->next_page();
  221. break;
  222. case '5':
  223. return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
  224. break;
  225. case '6':
  226. return $this->select();
  227. break;
  228. case '7':
  229. return $this->nowbar();
  230. break;
  231. }
  232. }
  233. /*----------------private function (私有方法)-----------------------------------------------------------*/
  234. /**
  235. * Set url header address
  236. * @param: String $url
  237. * @return boolean
  238. */
  239. function _set_url($url="")
  240. {
  241. if(!empty($url)){
  242. //手动设置
  243. $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
  244. }else{
  245. //自动获取
  246. if(empty($_SERVER['QUERY_STRING'])){
  247. //不存在QUERY_STRING时
  248. $this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
  249. }else{
  250. //
  251. if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){
  252. //地址存在页面参数
  253. $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER['REQUEST_URI']);
  254. $last=$this->url[strlen($this->url)-1];
  255. if($last=='?'||$last=='&'){
  256. $this->url.=$this->page_name."=";
  257. }else{
  258. $this->url.='&'.$this->page_name."=";
  259. }
  260. }else{
  261. //
  262. $this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
  263. }//end if
  264. }//end if
  265. }//end if
  266. }
  267. /**
  268. * Set current page
  269. *
  270. */
  271. function _set_nowindex($nowindex)
  272. {
  273. if(empty($nowindex)){
  274. //系统获取
  275. if(isset($_GET[$this->page_name])){
  276. $this->nowindex=intval($_GET[$this->page_name]);
  277. }
  278. if(isset($_POST['PB_Page_Select'])){
  279. $this->nowindex=$_POST['PB_Page_Select'];
  280. }
  281. }else{
  282. //手动设置
  283. $this->nowindex=intval($nowindex);
  284. }
  285. }
  286. /**
  287. * Return the address value for the specified page
  288. *
  289. * @param int $pageno
  290. * @return string $url
  291. */
  292. function _get_url($pageno=1)
  293. {
  294. return $this->url.$pageno;
  295. }
  296. /**
  297. * Get pagination display text, for example, by default _get_text('1') will return [1]
  298. *
  299. * @param String $str
  300. * @return string $url
  301. */
  302. function _get_text($str)
  303. {
  304. return $this->format_left.$str.$this->format_right;
  305. }
  306. /**
  307. * Get link address
  308. */
  309. function _get_link($url,$text,$style=''){
  310. $style=(empty($style))?'':'class="'.$style.'"';
  311. if($this->is_ajax){
  312. //如果是使用AJAX模式
  313. return ''.$text.'';
  314. }else{
  315. return ''.$text.'';
  316. }
  317. }
  318. /**
  319. * Error handling method
  320. */
  321. function error($function,$errormsg)
  322. {
  323. die('Error in file '.__FILE__.' ,Function '. $function.'() :'.$errormsg);
  324. }
  325. }
  326. // Inherit the paging class and add database access capabilities.
  327. class Page extends _Page {
  328. var $db; //db connected object
  329. var $_Sql_Query = ''; //Query the sql of the database
  330. var $_Total = 0; //The total records queried. Must first be
  331. var $_Rst = array(); //The records queried.
  332. / **
  333. * Paging query library.
  334. *
  335. * @param String $Sql Record query SQL statement.
  336. * @param int $pagenuber How many records per page.
  337. * @param int $pagen Current page.
  338. * @param String $url Parameters brought into the paging link. index.php?xx=b&bb=33
  339. * @param String $pname The mark of the current page. The default is index.php?xx=b&bb=33&page=2 if there are special requirements
  340. You can modify the parameters of $pname. For example: $pname='db_page', it becomes: index.php?xx=b&bb=33&db_page=2
  341. * @return Mysql_Page
  342. */
  343. function Page($db, $sql_query = '', $max_rows_per_page = 20, $current_page_number = 0, $url = '', $parameters = '', $pname = 'PB_page', $otc = '*') {
  344. $this -> db = $db;
  345. $pos_to = strlen($sql_query);
  346. $pos_from = strpos($sql_query, ' from', 0);
  347. $pos_group_by = strpos ($sql_query, ' group by', $pos_from);
  348. if (($pos_group_by < $pos_to) && ($pos_group_by != false)) $pos_to = $pos_group_by;
  349. $pos_having = strpos($sql_query, ' having ', $pos_from);
  350. if (($pos_having < $pos_to) && ($pos_having != false)) $pos_to = $pos_having;
  351. $pos_order_by = strpos($sql_query, ' order by', $pos_from);
  352. if (($pos_order_by < $pos_to) && ($pos_order_by != false)) $pos_to = $pos_order_by; ​​
  353. $reviews_count = $this -> db -> getResults("select count($otc) as total " . substr($sql_query, $pos_from, ($pos_to - $pos_from)));
  354. $query_num_rows = $reviews_count[0]['total'];
  355. $this -> _Total = $query_num_rows;
  356. $num_pages = ceil($query_num_rows / $max_rows_per_page);
  357. if ($current_page_number > $num_pages) {
  358. $current_page_number = $num_pages;
  359. }
  360. $offset = ($max_rows_per_page * ($current_page_number - 1));
  361. if ($offset < 0) $offset = 0;
  362. if ($offset > 0) {
  363. $offset = $offset + 1;
  364. }
  365. $this -> _Sql_Query = $sql_query . " limit " . $offset . ", " . $max_rows_per_page;
  366. $this -> setData(); //Query the database.
  367. parent :: page(array('total' => $query_num_rows, 'perpage' => $max_rows_per_page, 'page_name' = > $pname, 'url' => $url, 'parameters' => $parameters));
  368. }
  369. /**
  370. * Get the records of the current page and return an array.
  371. */
  372. function findByAll() {
  373. return $this -> _Rst;
  374. }
  375. /**
  376. * Display paging information.
  377. *
  378. * @param int $model
  379. */
  380. function dispaly_links($model) {
  381. $this -> show($model);
  382. }
  383. /**
  384. * Returns the number of records.
  385. *
  386. * @return Int
  387. */
  388. function getCount() {
  389. return $this -> _Total;
  390. }
  391. /**
  392. * Get the number of query result records..
  393. *
  394. * @return Int
  395. */
  396. function getRows() {
  397. return count($this -> _Rst);
  398. }
  399. /**
  400. * Execute query function.
  401. * Calculate array.
  402. * Private method.
  403. */
  404. function setData() {
  405. $this -> _Rst = $this -> db -> getResults($this -> _Sql_Query);
  406. }
  407. }
  408. ?>
Copy code


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