PHP Pagination Class Code

Source: Internet
Author: User

/* Idea 1. Get the URL of the Address bar 2. Parses the query part of the URL--that is? Part of the 3.query section of the argument is parsed into the array of 4. Put the page element in the arrays, +1,-1, Form 2 new Array 5. Then the new array is stitched into the query section, the previous page, the next page, the connection Address *///page class page {public $total;//all bars, remove public from the database $prePage = 10; Number of bars per page protected $curr = 1;//Default current page number public function __construct ($total, $prePage = ") {$this->total = $total;// Gross position entry information is placed in the total property if ($prePage > 0) {$this->prepage = $prePage;//Place the number of pages per page in the Perpage attribute}//calculate the current page number if (Isset ($_get[' page ')) && ($_get[' page ' + 0) > 0) {$this->curr = $_get[' page '] + 0;}} Principal functions Public Function showpage () {if ($this->total <=0) {return ';//If the total entry <=0 directly returns an empty string} $cnt = Ceil ($this->to tal/$this->prepage); Count the total number of pages, go to the whole///According to the current page, Count previous page, next page/* Parse URL, how many cases? xx.phpxx.php?id=5xx.php?page=3xx.php?id=5&page=3*///final generated URL inside must have Page=n$url = $_server[' REQUEST_URI ']; $parse = Parse_url ($url); Put the URL analysis results in the array//print_r ($parse);//Ensure that the parameters have Pageif (!isset ($parse [' query ']) {$parse [' query '] = ' page= '. $this Curr;} To parse the query string into groups, again ensure that there is a page option parse_str ($parse [' query '], $parms); if (!arRay_key_exists (' page ', $parms)) {$parms [' page '] = $this->curr;} The top four cases are tested once, page parameters can be generated//print_r ($parms);//Judging in addition to the page, there is no other parameter if (count ($parms) = = 1) {$url = $parse [' Path ']. ‘?‘;} else {unset ($parms [' page ']), $url = $parse [' Path ']. ‘?‘ . Http_build_query ($parms). ' & ';} echo $url $prev = $this->curr-1; $next = $this->curr + 1;//Home $indexlink = ' <a href= '. $url. ' Page= '. 1. ' > Home </a> ';//previous if ($prev < 1) {$prevLink = ';} else {$prevLink = ' <a href= '. $url. ' Page= '. $prev. ' > Prev </a> ';} Next if ($next > $cnt) {$nextLink = ';} else {$nextLink = ' <a href= '. $url. ' Page= '. $next. ' > Next </a> ';} Last $lastlink = ' <a href= '. $url. ' Page= '. $cnt. ' > Last </a> ';//echo $indexLink. '    '. $prevLink. '    '. $nextLink. '    '. $lastLink;//prev, 1 2 3 4 5 Next $start = $this->curr-(5-1)/2; Calculates the page number starting on the left $end = $this->curr + (5-1)/2;//calculates the page number that starts on the right///If the left page is already less than 1, then the portion less than 1 is to the right if ($start < 1) {$end + = (1-$start); $start = 1; Modify start = 1if ($end > $cnt) {$end = $cnt;}} Fill to the left if ($end > $cnt) {$start-= ($end-$cnt), $end = $cnt, if ($start < 1) {$start = 1;}} Loop out page number $pagestr = '; for ($i = $start; $i <= $end; $i + +) {if ($i = = $this->curr) {$pageStr. = $i; continue;} $pageStr. = ' <a href= '. $url. ' Page= '. $i. ' > '. $i. ' </a> ';} Return $indexLink. $prevLink. $pageStr. $nextLink. $lastLink;}} $page = new page (30,3); Echo $page->showpage ();

PHP Pagination Class Code

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.