I personally learned a simple paging class written in PHP, which has strong versatility.
- /**
- * Simple paging class
- * @author:phpprince
- * @QQ: 8923052
- * @date: 2014-04-22 21:08:35
- */
- class Page{
- protected $url; //Address
- protected $allnum; //Total number of records
- protected $current; //Current page number
- protected $pagesize; //How many records are displayed on each page
- protected $postfix; //Suffix
- protected $style; //There are 3 display styles, 1 2 3 respectively means the first 5 and the last 4, the first 3 and the last 3, and the first 4 Post 4
-
- public function __construct($url,$allnum,$current,$pagesize=10,$postfix='',$style=1){
- $this->url=$url;
- $this-> ;allnum=$allnum;
- $this->current=$current;
- $this->pagesize=$pagesize;
- $this->postfix=$postfix;
- $this->style=$style;
- }
-
- //Get the total number of pages
- protected function maxPageNum(){
- $max=ceil($this->allnum/$this->pagesize);
- //Page number over limit correction
- if($this ->current>$max){
- $this->current=$max;
- }
- if($this->current<1){
- $this->current=1;
- }
- return $ max;
- }
-
- //Get the complete html of the first page link str
- protected function firstUrl(){
- if($this->current!=1)
- {
- return 'Homepage';
- }else{
- return 'Homepage';
- }
- }
-
- //Get the complete html of the previous page link str
- protected function prevUrl() {
- if($this->current<=1){
- $fullurl='previous page';
- }else{
- $fullurl=$this->url.($this->current-1).$this->postfix;
- $fullurl='Previous page';
- }
- return $fullurl;
- }
-
- //Get next One page link complete html str
- protected function nextUrl(){
- if($this->current>=$this->maxPageNum()){
- $fullurl='< ;a>Next page';
- }else{
- $fullurl=$this->url.($this->current+1).$this->postfix ;
- $fullurl='Next page span>';
- }
- return $fullurl;
- }
-
- //Get the complete html of the last page link str
- protected function lastUrl(){
- if($this->current>=$this->maxPageNum( )){
- $fullurl='lastpage';
- }else{
- $fullurl=$this->url .$this->maxPageNum().$this->postfix;
- $fullurl='Last page';
- }
- return $fullurl;
- }
-
- //Get the complete url of the specified page number
- protected function getUrl($pageno){
- return $this- >url.$pageno.$this->postfix;
- }
-
- //Specify the display style
- protected function getStyle(){
- switch($this->style){
- case 1:
- $before=5 ;
- $after=4;
- break;
- case 2:
- $before=3;
- $after=3;
- break;
- case 3:
- $before=4;
- $after=4;
- break;
- default :
- $before=5;
- $after=4;
- }
-
- return array($before,$after);
- }
-
- //Get the intermediate URL 1 2 3 4 5 ⑥ 7 8 9 10
- protected function getMiddelUrl (){
- $max=$this->maxPageNum(); //Get the total page first to correct the current page overrun problem
- $current=$this->current; //The current page number must be legal to ensure The following page number range must be correct
- //Get the current style
- list($before,$after)=$this->getStyle();
- $startno=$current-$before; //Start page number
- $endno= $current+$after; //End page number
-
- //To ensure that the output always meets the requirements, if the starting page increases, the end page must increase, and vice versa.
- while($endno>$ max||$startno<1){
- if($endno>$max){ //The end page number is out of bounds
- $endno--;
- if($startno>1){
- $startno--;
- }
- }
- if($startno<1){ //The starting page number is out of bounds
- $startno++;
- if($endno<$max){
- $endno++;
- }
- }
- }
- $str=''; //Use To save the entire html str
- for($i=$startno;$i<=$endno;$i++){
- $currenturl=$this->getUrl($i);
- if($i!=$current) {
- $str.="{$i}";
- }else{
- $str.=''.$i.'';
- }
- }
- return $str;
- }
-
- //Return the complete paging html string
- public function getPageStr() {
- $str='
'.$this->firstUrl().$this->prevUrl();
- $str.=$this->getMiddelUrl();
- $str.=$this->nextUrl().$this->lastUrl().'total'.$this->maxPageNum().'page'. $this->allnum.'bar
';
- return $str;
- }
- }
- ?>
Copy code
|