给大家分享一个自己写的PHP分页类,使用方法简单,而且在网页中,结合Smarty可以实现很丰富的效果,例如滑动门、翻页照片等效果。想的到的就自己实现吧,这里把源码分享下。
1 CreateUriPara (); 返回参数数组,array['Prev'],array['ParaFirst'],array['ParaEnd'],array['Next'] 7 * @author modofu.chen 8 * @copyright 2013 9 */ 10 defined(PAGE_SIZE) or define (PAGE_SIZE , 10); 11 defined(PAGE_IDENTIFY) or define (PAGE_IDENTIFY , "P"); 12 class Page 13 { 14 private static $__PageIdentify; //URL中的页码标志符号 15 private static $__PageSize; //每页显示数目 16 private static $__RecordCount; //总记录数 17 private static $__PagePara; //页数 18 private static $__CurrentPage; //当前页码 19 private static $__CurrentPara; //当前页码所属的段,比如显示1~10,11~20,这便是段 20 public function __construct($_PageIdentify = PAGE_IDENTITY , $_PageSize = PAGE_SIZE , $_RecordCount) 21 { 22 self::__initial ($_PageIdentify , $_PageSize , $_RecordCount); 23 } 24 25 private function __initial ($_PageIdentify , $_PageSize , $_RecordCount) 26 { 27 self::__set ('PageIdentify' , $_PageIdentify); 28 self::__set ('PageSize' , $_PageSize); 29 self::__set ('RecordCount' , $_RecordCount); 30 self::__set ('PagePara' , NULL); 31 self::__set ('CurrentPage' , $_GET[self::$__PageIdentify]); 32 self::__set ('CurrentPara' , NULL); 33 } 34 35 private function setPageIdentify ($_PageIdentify) 36 { 37 self::$__PageIdentify = !empty ($_PageIdentify) ? $_PageIdentify : PAGE_IDENTIFY; 38 } 39 private function getPageIdentify () 40 { 41 return self::$__PageIdentify; 42 } 43 44 private function setPageSize ($_PageSize) 45 { 46 self::$__PageSize = (!empty ($_PageSize) && is_numeric ($_PageSize)) ? $_PageSize : PAGE_SIZE; 47 } 48 private function getPageSize () 49 { 50 return self::$__PageSize; 51 } 52 53 private function setRecordCount ($_RecordCount) 54 { 55 self::$__RecordCount = (!empty ($_RecordCount) && is_numeric ($_RecordCount)) ? $_RecordCount : 0; 56 } 57 private function getRecordCount () 58 { 59 return self::$__RecordCount; 60 } 61 62 private function setPagePara () 63 { 64 65 self::$__PagePara = ceil (self::$__RecordCount/self::$__PageSize); 66 } 67 private function getPagePara () 68 { 69 return self::$__PagePara; 70 } 71 72 private function setCurrentPage ($_CurrentPage) 73 { 74 self::$__CurrentPage = (!empty($_CurrentPage) && is_numeric ($_CurrentPage) && (ceil(($_CurrentPage)/self::$__PageSize) <= self::$__PagePara )) ? $_CurrentPage : 1 ; 75 } 76 private function getCurrentPage () 77 { 78 echo self::$__CurrentPage; 79 return self::$__CurrentPage; 80 } 81 82 private function setCurrentPara () 83 { 84 self::$__CurrentPara = ceil (self::$__CurrentPage/self::$__PageSize); 85 } 86 public function getCurrentPara () 87 { 88 return self::$__CurrentPara; 89 } 90 91 private function __set($name,$value) 92 { 93 if(!empty ($name)) 94 { 95 $functionname = 'set'.$name; 96 self::$functionname ($value); 97 } 98 } 99 private function __get ($name)100 {101 if (!empty ($name))102 {103 $functionname = 'get'.$name;104 $this-> $functionname ($name);105 }106 }107 108 public function CreateUriPara ()109 {110 $UriArray = array(111 'Prev' => (((self::$__CurrentPara - 2) * self::$__PageSize + 1) > 0 ? ((self::$__CurrentPara - 2) * self::$__PageSize + 1) : 1),112 'ParaFirst' => (((self::$__CurrentPara - 1) * self::$__PageSize) > 0 ? ((self::$__CurrentPara - 1) * self::$__PageSize) : 1),113 'ParaEnd' => (self::$__CurrentPara * self::$__PageSize < self::$__RecordCount ? self::$__CurrentPara * self::$__PageSize : self::$__RecordCount),114 'Next' => ((self::$__CurrentPara * self::$__PageSize +1) < self::$__RecordCount ? (self::$__CurrentPara * self::$__PageSize +1) : self::$__RecordCount),115 );116 return $UriArray;117 118 }119 }120 121 ?>
使用方法:
$Page = new Page('PageIdentify' , Pagesize , RecordCount);$array = $Page -> CreateUriPara (); //返回参数数组,array['Prev'],array['ParaFirst'],array['ParaEnd'],array['Next']
$array['Prev']:上一页;
$array['ParaFirst']:每段的第一页;
$array['ParaEnd']:每段的最后一页;
$array['Next']:下一页。
源码不作分析。