PHP Universal Paging class sharing

Source: Internet
Author: User
This article is mainly for everyone to introduce the universal PHP paging class, with a certain reference value, interested in small partners can refer to

The example of this article for everyone to share the PHP page of the specific code, for your reference, the specific content as follows


<?php/* Core: Home, previous page, next page, last url*//* super global $_server*/$page = new page (5,60); Var_dump ($page->allurl ()); class page{//    The number of protected per page is $number;//How much data protected $totalCount;//current page protected $page;//URL protected $url;    Public function __construct ($number, $totalCount) {$this->number= $number;    $this->totalcount = $totalCount;    Get total pages $this->totalpage = $this->gettotalpage ();    Gets the current page $this->page = $this->getpage ();    Get URL $this->url = $this->geturl ();  Echo $this->url;  }/* Get total pages and rounding up */protected function gettotalpage () {return ceil ($this->totalcount/$this->number);    }/**/protected function GetPage () {if (Empty ($_get[' page ')) {$page = 1;    }elseif ($_get[' page '] > $this->totalpage) {$page = $this->totalpage;    }elseif ($_get["page"]<1) {$page = 1;    }else{$page = $_get[' page ');  } return $page; } protected function GetUrl () {//Get protocol name $scheme = $_server[' REquest_scheme '];    Get host name $host = $_server[' server_name ');    Get the port number $port = $_server[' Server_port ');    Get the path and request string $url = $_server[' Request_uri '); /* Middle do processing, want to page=5 such string concatenation URL, so if the original URL has page This parameter, we first need to empty the original page parameter */$urlArray = Parse_url ($url);//Var_    Dump ($urlArray);    $path = $urlArray [' path '];      if (!empty ($urlArray [' query '])) {//Converts the value in query to an array parse_str ($urlArray [' query '], $array);      If he has a page, delete it unset ($array [' page ']);      Convert associative arrays to query $query = http_build_query ($array); If not empty, connect with path if ($query! = ") {$path = $path. '? '.      $query; }} return ' http://'.  $host. ': ' $port. $path; } protected function SetUrl ($STR) {if (Strstr ($this->url, '? ')) {$url = $this->url. '    & '. $str; }else{$url = $this->url. '? '.    $STR;  } return $url; }/* All url*/Public Function Allurl () {return [' first ' = = $this->first (), ' next ' = $this->nex T (), ' prev ' =&GT  $this->prev (), ' end ' = $this->end (),];  }/* Home */Public Function first () {return $this->seturl (' page=1 ');  }/* Next page */Public function Next () {//According to the page number under the current page if ($this->page+1 > $this->totalpage) {$page =    $this->totalpage;    }else{$page = $this->page+1;  } return $this->seturl (' page= '. $page);    }/* Previous page */Public Function prev () {//According to the page number under the current page if ($this->page-1 < 1) {$page = 1;    }else{$page = $this->page-1;  } return $this->seturl (' page= '. $page);  }/* Last */Public function end () {return $this->seturl (' page= '. $this->totalpage);    }/*limit 0,5*/Public function limit () {$offset = ($this->page-1) * $this->number;  return $offset. ', '. $this->number; }  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.