一個不錯的php分頁類的代碼

來源:互聯網
上載者:User
  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: 分頁類,四種分頁模式
  8. * 2.0增加功能:支援自訂風格,自訂樣式,同時支援PHP4和PHP5,
  9. * to see detail,please visit http://www.phpobject.net/blog/read.php?
  10. * example:
  11. * 模式四種分頁模式:
  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. 開啟AJAX:
  19. $ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
  20. echo 'mode:1
    '.$ajaxpage->show();
  21. 採用繼承自訂分頁顯示模式:
  22. demo:http://www.phpobject.net/blog
  23. */
  24. class page
  25. {
  26. /**
  27. * config ,public
  28. */
  29. var $page_name="PB_page";//page標籤,用來控制url頁
  30. var $next_page='>';//下一頁
  31. var $pre_page='<';//上一頁
  32. var $first_page='First';//首頁
  33. var $last_page='Last';//尾頁
  34. var $pre_bar='<<';//上一分頁條
  35. var $next_bar='>>';//下一分頁條
  36. var $format_left='[';
  37. var $format_right=']';
  38. var $is_ajax=false;//是否支援AJAX分頁模式

  39. /**

  40. * private
  41. *
  42. */
  43. var $pagebarnum=10;//控制記錄條的個數。
  44. var $totalpage=0;//總頁數
  45. var $ajax_action_name='';//AJAX動作名
  46. var $nowindex=1;//當前頁
  47. var $url="";//url地址頭
  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']);//設定pagename
  71. $this->_set_nowindex($nowindex);//設定當前頁
  72. $this->_set_url($url);//設定連結地址
  73. $this->totalpage=ceil($total/$perpage);
  74. $this->offset=($this->nowindex-1)*$perpage;
  75. if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//開啟AJAX模式
  76. }
  77. /**
  78. * 設定類中指定變數名的值,如果改變數不屬於這個類,將throw一個exception
  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. * 開啟倒AJAX模式
  93. *
  94. * @param string $action 預設ajax觸發的動作。
  95. */
  96. function open_ajax($action)
  97. {
  98. $this->is_ajax=true;
  99. $this->ajax_action_name=$action;
  100. }
  101. /**
  102. * 擷取顯示"下一頁"的代碼
  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. * 擷取顯示“上一頁”的代碼
  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. * 擷取顯示“首頁”的代碼
  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. * 擷取顯示“尾頁”的代碼
  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. * 擷取顯示跳轉按鈕的代碼
  175. *
  176. * @return string
  177. */
  178. function select()
  179. {
  180. $return=''; for($i=1;$i<=$this->totalpage;$i++) { if($i==$this->nowindex){ $return.=''.$i.''; }else{ $return.=''.$i.''; } } unset($i); $return.='';
  181. return $return;
  182. }

  183. /**

  184. * 擷取mysql 語句中limit需要的值
  185. *
  186. * @return string
  187. */
  188. function offset()
  189. {
  190. return $this->offset;
  191. }

  192. /**

  193. * 控制分頁顯示風格
  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. * 設定url頭地址
  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. * 設定當前頁面
  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. * 為指定的頁面返回地址值
  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_text('1')將返回[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. * 擷取連結地址
  303. */
  304. function _get_link($url,$text,$style=''){
  305. $style=(empty($style))?'':'class="'.$style.'"';
  306. if($this->is_ajax){
  307. //如果是使用AJAX模式
  308. return 'ajax_action_name.'(\''.$url.'\')">'.$text.'';
  309. }else{
  310. return ''.$text.'';
  311. }
  312. }
  313. /**
  314. * 出錯處理方式
  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. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.