php分頁類調用執行個體教程

來源:互聯網
上載者:User
  1. /**

  2. * filename: ext_page.class.php
  3. * @package:phpbean
  4. * description:超強分頁類,四種分頁模式,預設採用類似baidu,google的分頁風格。
  5. * 2.0增加功能:支援自訂風格,自訂樣式,同時支援PHP4和PHP5,
  6. * example:
  7. * 模式四種分頁模式:
  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. 開啟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. 採用繼承自訂分頁顯示模式:
  18. */
  19. class Page
  20. {
  21. /**
  22. * config ,public
  23. */
  24. var $page_name="p";//page標籤,用來控制url頁。比如說xxx.php?PB_page=2中的PB_page
  25. var $next_page='>';//下一頁
  26. var $pre_page='<';//上一頁
  27. var $first_page='First';//首頁
  28. var $last_page='Last';//尾頁
  29. var $pre_bar='<<';//上一分頁條
  30. var $next_bar='>>';//下一分頁條
  31. var $format_left='';
  32. var $format_right='';
  33. var $is_ajax=false;//是否支援AJAX分頁模式

  34. /**

  35. * private
  36. *
  37. */
  38. var $pagebarnum=10;//控制記錄條的個數。
  39. var $totalpage=0;//總頁數
  40. var $ajax_action_name='';//AJAX動作名
  41. var $nowindex=1;//當前頁
  42. var $url="";//url地址頭
  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']);//設定pagename
  66. $this->_set_nowindex($nowindex);//設定當前頁
  67. $this->_set_url($url);//設定連結地址
  68. $this->totalpage=ceil($total/$perpage);
  69. $this->offset=($this->nowindex-1)*$perpage;
  70. if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//開啟AJAX模式
  71. }
  72. /**
  73. * 設定類中指定變數名的值,如果改變數不屬於這個類,將throw一個exception
  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. * 開啟倒AJAX模式
  88. *
  89. * @param string $action 預設ajax觸發的動作。
  90. */
  91. function open_ajax($action)
  92. {
  93. $this->is_ajax=true;
  94. $this->ajax_action_name=$action;
  95. }
  96. /**
  97. * 擷取顯示"下一頁"的代碼
  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. * 擷取顯示“上一頁”的代碼
  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. * 擷取顯示“首頁”的代碼
  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. * 擷取顯示“尾頁”的代碼
  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. * 擷取顯示跳轉按鈕的代碼
  170. *
  171. * @return string
  172. */
  173. function select()
  174. {
  175. $return=''; for($i=1;$i<=$this->totalpage;$i++) { if($i==$this->nowindex){ $return.=''.$i.''; }else{ $return.=''.$i.''; } } unset($i); $return.='';
  176. return $return;
  177. }

  178. /**

  179. * 擷取mysql 語句中limit需要的值
  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 'ajax_action_name.'(\''.$url.'\')">'.$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. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.