A simple universal paging class written by myself
Release: 2016-07-25 08:47:57
Original
952 people have browsed it
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
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31