分享:一例PHP翻頁(分頁)類別的實例代碼

WBOY
發布: 2016-07-25 08:52:25
原創
947 人瀏覽過
  1. /**
  2. * filename: ext_page.class.php
  3. * @package:phpbean
  4. * descrīption:超強分頁類,四種分頁模式,預設為類似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:
  15. $ajaxpage=new page(array( 'total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
  16. echo 'mode:1
    '.$ajaxpage- >show();
  17. 採用繼承自訂分頁顯示模式。
  18. 編輯整理:腳本學堂 http://bbs.it-home.org
  19. */
  20. class _page
  21. {
  22. /**
  23. * 配置,公用
  24. class _page
  25. {
  26. /**
  27. * 私人
  28. *
  29. /
  30. var $page_name="PB_page";//page標籤,用來控制url頁。例如說xxx.php?PB_page=2中的PB_page
  31. var $next_page='>';//下一頁
  32. var $pre_page=' var $first_page ='First';//首頁
  33. var $last_page='Last';//尾頁
  34. var $pre_bar=' var $next_bar='> >';//下一分頁條
  35. var $format_left='[';
  36. var $format_right=']';
  37. var $is_ajax=false;//是否支援AJAX分頁模式
  38. /**
  39. * 建構子建構子
  40. *
  41. * @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'] , $array['ajax']...
  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. * 設定類別中指定變數名稱的值,如果改變量不屬於這個類,將throw一個exception
  51. *
  52. * @param string $var
  53. * @param string $value
  54. */
  55. function page($array)
  56. {
  57. if(is_array( $array)){
  58. if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a param of total');
  59. $total=intval($array[' total']);
  60. $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
  61. $nowindex=(array_key_exists('nowindex',$ array))?intval($array['nowindex']):'';
  62. $url=(array_key_exists('url',$array))?$array['url']:'';
  63. }else{
  64. $total=$array;
  65. $perpage=10;
  66. $nowindex='';
  67. $url='';
  68. }
  69. if((!is_int ($total))||($totalerror(__FUNCTION__,$total.' is not a positive integer!');
  70. if((!is_int($perpage))|| ($perpageerror(__FUNCTION__,$perpage.' is not a positive integer!');
  71. if(!empty($array['page_name']))$this-> set('page_name',$array['page_name']);//設定pagename
  72. $this->_set_nowindex($nowindex);//設定目前頁面
  73. $this->_set_url($url); //設定連結位址
  74. $this->totalpage=ceil($total/$perpage);
  75. $this->offset=($this->nowindex-1)*$perpage;
  76. if( !empty($array['ajax']))$this->open_ajax($array['ajax']);//開啟AJAX模式
  77. }
  78. /***/
  79. function set($var,$value)
  80. {
  81. if(in_array($var,get_object_vars($this)))
  82. $this->$var=$value;
  83. else {
  84. $this->$var=$value;
  85. else {
  86. $this->error(__FUNCTION__,$var." does not belong to PB_Page!");
  87. }
  88. } /** * 開啟倒AJAX模式 * * @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->nowindextotalpage) {
  104. 回傳$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. 回傳$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='')
  146. {
  147. $plus=ceil($this->pagebarnum/2);
  148. if($this->pagebarnum-$plus $this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage $this->現在索引);
  149. $begin=$this->nowindex-$plus 1
  150. $begin=($begin>=1)?$begin:1;
  151. $return=''; $begin;$ipagebarnum;$i )
  152. {
  153. if($itotalpage){
  154. if($i!=$this-> nowindex)
  155. $return.= $this->_get_text($this->_get_link($this->_get_url($i),$i,$style ));
  156. else
  157. $return.= $this->_get_text(''.$i.'');
  158. }其他{
  159. 中斷;
  160. }
  161. $return.="n";
  162. }
  163. 取消設置($begin);
  164. 返回$return;
  165. /**
  166. * 取得顯示跳轉鈕的程式碼
  167. *
  168. * @return string
  169. */
  170. 函數選擇()
  171. {
  172. $return=' ';
  173. 回傳$return;
  174. /**
  175. * 取得mysql 語句中limit所需的值
  176. *
  177. * @return string
  178. */
  179. function offset()
  180. {
  181. return $this ->offset;
  182. }
  183. /**
  184. * 페이징 표시 스타일 제어(해당 스타일 추가 가능)
  185. *
  186. * @param int $mode
  187. * @return string
  188. */
  189. function show($mode=1)
  190. {
  191. 스위치($mode)
  192. {
  193. case '1':
  194. $this->next_page='下一页';
  195. $this->pre_page='상일页';
  196. return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
  197. 휴식;
  198. 사례 '2':
  199. $this->next_page='下一页';
  200. $this->pre_page='상일页';
  201. $this->first_page='首页';
  202. $this->last_page='尾页';
  203. return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this ->last_page().'第'.$this->select().'页';
  204. 휴식;
  205. 사례 '3':
  206. $this->next_page='下一页';
  207. $this->pre_page='상일页';
  208. $this->first_page='首页';
  209. $this->last_page='尾页';
  210. return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
  211. 휴식;
  212. 사례 '4':
  213. $this->next_page='下一页';
  214. $this->pre_page='상일页';
  215. return $this->pre_page().$this->nowbar().$this->next_page();
  216. 휴식;
  217. 케이스 '5':
  218. return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this ->next_bar();
  219. 휴식;
  220. 케이스 '6':
  221. return $this->select();
  222. 휴식;
  223. 케이스 '7':
  224. return $this->nowbar();
  225. 휴식;
  226. }
  227. }
  228. /*---프라이빗 함수(私유방법)------------ ---------------------*/
  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.((stristr($url,'?'))?'&':'?').$this->page_name."=";
  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->페이지_이름]);
  272. }
  273. if(isset($_POST['PB_Page_Select'])){
  274. $this->nowindex=$_POST['PB_Page_Select'];
  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-> 형식_오른쪽;
  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 ''.$text.'';
  309. }else{
  310. return ''.$text.'';
  311. }
  312. }
  313. /**
  314. * 오류 처리
  315. */
  316. 함수 오류($function,$errormsg)
  317. {
  318. die('파일 '.__FILE__에 오류가 있습니다.' ,Function '.$function.'() :'.$errormsg)
  319. }
  320. }
  321. // 페이징 클래스 상속 및 데이터베이스 액세스 추가 Capability .
  322. class Page 확장 _Page {
  323. var $db; //db 연결 개체
  324. var $_Sql_Query = '' //데이터베이스의 SQL 쿼리
  325. var $_Total = 0; /쿼리 전체 레코드는
  326. var $_Rst = array(); //쿼리된 레코드입니다.
  327. /**
  328. * 페이징 쿼리 라이브러리
  329. *
  330. * @param String $Sql 레코드 쿼리용 SQL 문
  331. * @param int $pagenuber 페이지당 레코드 수
  332. * @param int $ pagen 현재 페이지
  333. * @param String $url 페이징 링크에서 가져온 매개변수 index.php?xx=b&bb=33
  334. * @param String $pname 현재 페이지의 표시, 기본값은 index입니다. .php?xx=b&bb=33&page=2 특별한 요구사항이 있는 경우
  335. $pname의 매개변수를 수정할 수 있습니다. 예: $pname='db_page', index.php?xx=b&bb=33&db_page= 2
  336. * @return Mysql_Page
  337. */
  338. 함수 페이지($db, $ sql_query = '', $max_rows_per_page = 20, $current_page_number = 0, $url = '', $parameters = '', $pname = 'PB_page',$otc = '*') {
  339. $this -> db = $db;
  340. $pos_to = strlen($sql_query);
  341. $pos_from = strpos($sql_query, 'from', 0)
  342. $pos_group_by = strpos($sql_query, '그룹별' , $ pos_from)
  343. if (($pos_group_by < $pos_to) && ($pos_group_by != false)) $pos_to = $pos_group_by
  344. $pos_having = strpos($sql_query, 'having', $pos_from );
  345. if (($pos_having < $pos_to) && ($pos_having != false)) $pos_to = $pos_having
  346. $pos_order_by = strpos($sql_query, 'order by', $pos_from);
  347. if (($pos_order_by < $pos_to) && ($pos_order_by != false)) $pos_to = $pos_order_by; ​​​​
  348. $reviews_count = $this -> ($otc) as total " . substr($sql_query, $pos_from, ($pos_to - $pos_from)));
  349. $query_num_rows = $reviews_count[0]['total'];
  350. $this -> ; _Total = $ query_num_rows;
  351. $num_pages = ceil($query_num_rows / $max_rows_per_page)
  352. if ($current_page_number > $num_pages) {
  353. $current_page_number = $num_pages; $offset = ( $max_rows_per_page * ($current_page_number - 1));
  354. if ($offset < 0) $offset = 0
  355. if ($offset > 0) {
  356. $offset = $ 오프셋 1; $this -> 데이터베이스
  357. parent :: page(array('total' => $query_num_rows, 'perpage' => $max_rows_per_page, 'page_name' => $pname, 'url' => $url, 'parameters) ' => $ 매개변수));
  358. }
  359. /**
  360. * 현재 페이지의 기록을 가져와서 배열을 반환합니다.
  361. */
  362. function findByAll() {
  363. return $this ->
  364. /* *
  365. * 페이징 정보 표시
  366. *
  367. * @param int $model
  368. */
  369. function dispaly_links($model) {
  370. $this -> show($model)
  371. }
  372. /**
  373. * 레코드 수를 반환합니다.
  374. *
  375. * @return Int
  376. */
  377. function getCount () {
  378. return $this ->
  379. }
  380. /**
  381. * 쿼리 결과 레코드 개수를 가져옵니다..
  382. *
  383. * @return Int
  384. */
  385. function getRows() {
  386. return count($this -> _Rst) ;
  387. }
  388. /**
  389. * 쿼리 기능 실행
  390. * 배열 계산
  391. * 프라이빗 메소드
  392. */
  393. function setData() {
  394. $this -> $this -> _Sql_Query)
  395. }
  396. }
  397. ?>
  398. 코드 복사
  399. >
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!