PHP ページング クラスの呼び出し例のチュートリアル

WBOY
リリース: 2016-07-25 08:52:36
オリジナル
754 人が閲覧しました
  1. /**

  2. * ファイル名: ext_page.class.php
  3. * @package:phpbean
  4. * 説明: 超強力なページング クラス、4 つのページング モード、デフォルトのページング スタイルは Baidu や Google に似ています。
  5. * 2.0 追加機能: カスタム スタイルのサポート、カスタム スタイル、PHP4 と PHP5 の両方のサポート、
  6. * 例:
  7. * 4 つのページング モード:
  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 '< hr>mode:2
    '.$page->show(2);
  12. echo '
    mode:3
    '.$page->show(3);
  13. echo '< hr>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. * 設定、公開
  23. */
  24. var $page_name="p";//page タグ、URL ページを制御するために使用されます。たとえば、xxx.phpのPB_page?PB_page=2
  25. var $next_page='>';//次のページ
  26. var $pre_page='<';//前のページ
  27. var $first_page='First' ;/ /Homepage
  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. *プライベート
  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. 関数ページ($array)
  50. {
  51. if(is_array($array)){
  52. //if(! array_key_exists ('total',$array))$this->error(__FUNCTION__,'total のパラメータが必要です');
  53. $total=intval($array['total']);
  54. $perpage=(array_key_exists( ' ページごと',$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 if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' は正の整数ではありません!');
  64. if (!empty($array['page_name']))$this->set('page_name',$array['page_name']);//ページ名を設定します
  65. $this->_set_nowindex($nowindex); / /現在のページを設定します
  66. $this->_set_url($url);//リンクアドレスを設定します
  67. $this->gt;totalpage=ceil($total/$perpage);
  68. $this->offset=( $this- >nowindex-1)*$perpage;
  69. if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//AJAX モードを開く
  70. }
  71. / **
  72. * 指定された変数名の値をクラスに設定します。変更がこのクラスに属さない場合、例外がスローされます
  73. *
  74. * @param string $var
  75. * @param string $value
  76. */
  77. function set($var,$value)
  78. {
  79. if(in_array($var,get_object_vars($this)))
  80. $this->$var=$value;
  81. else {
  82. $this->error(__FUNCTION__,$var." は PB_Page に属しません!");
  83. }

  84. }

  85. /**
  86. * 逆 AJAX モードをオンにします
  87. *
  88. * @ param string $action デフォルトの Ajax トリガーアクション。
  89. */
  90. function open_ajax($action)
  91. {
  92. $this->is_ajax=true;
  93. $this->ajax_action_name=$action;
  94. }
  95. /**
  96. * 「次のページ」を表示するコードを取得します
  97. *
  98. * @param string $style
  99. * @return string
  100. */
  101. function next_page($style ='')
  102. {
  103. if($this->nowindex<$this->totalpage){
  104. return $this->_get_link($this->_get_url($this->nowindex+1), $this->next_page,$style);
  105. }
  106. return ''.$this->next_page.'';
  107. }

  108. /**

  109. * 「前のページ」を表示するコードを取得します
  110. *
  111. * @param string $style
  112. * @return string
  113. */
  114. function pre_page($style='')
  115. {
  116. if($this->nowindex>1){
  117. return $this->_get_link($this->_get_url($ this->nowindex-1),$this->pre_page,$style);
  118. }
  119. return ''.$this->pre_page.' ';
  120. }

  121. /**

  122. * 「ホームページ」を表示するコードを取得します
  123. *
  124. * @return string
  125. */
  126. function first_page($style='')
  127. {
  128. if($this->nowindex==1){
  129. return ''.$this->first_page.'';
  130. }
  131. return $this->_get_link($this->_get_url(1),$this->first_page,$style) ;
  132. }

  133. /**

  134. * 「最後のページ」を表示するコードを取得します
  135. *
  136. * @return string
  137. */
  138. function last_page($style='')
  139. {
  140. if($this->nowindex==$this->totalpage){
  141. return ''.$this->last_page.'';
  142. }
  143. return $this->_get_link($this->_get_url($this->totalpage), $this->last_page,$style);
  144. }

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

  146. {
  147. $plus=ceil($this->pagebarnum/2);
  148. if($this->pagebarnum-$plus+$this-> ;nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
  149. $begin=$this->nowindex-$plus+1 ;
  150. $begin=($begin>=1)?$begin:1;
  151. $return='';
  152. for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
  153. {
  154. if($i<=$this->totalpage){
  155. if($i!=$this->nowindex)
  156. $return.=$this->_get_text($this->_get_link($ this->_get_url($i),$i,$style));
  157. else
  158. $return.=$this->_get_text(''. $i.'');
  159. }else{
  160. Break;
  161. }
  162. $return.="n";
  163. }
  164. unset($begin);
  165. return $return;
  166. }
  167. /**
  168. * ジャンプボタンを表示するコードを取得します
  169. *
  170. * @return string
  171. */
  172. function select()
  173. {
  174. $return='';
  175. return $return;
  176. }

  177. /**

  178. * mysql ステートメントの制限に必要な値を取得します
  179. *
  180. * @return string
  181. */
  182. function offset()
  183. {
  184. return $this->offset;
  185. }

  186. /**

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

  226. }

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

  261. /**

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

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

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

  277. /**

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

  287. /**

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

  297. /**

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

复制代码


関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!