PHP Universal Paging (Pager) class

Source: Internet
Author: User

Three different modes of display attached style~


1.1
2.2
3.3


4. Paging Class body

<?php/** * PHP Universal Paging class * * Show (2) 1 ... 62 63 64 65 66 67 68 ... 150 * Pagination style * #page {font:12px/16px arial} * #page span{float:left;margin:0px 3px;} * #page a{float:left;margin:0 3px;border:1px Solid #ddd;p adding:3px * 7PX; Text-decoration:none;color: #666} * #page a.now_page, #page a:hover{color: #fff; background: #05c} */class Pager {public $ First_row; Number of starting lines public $list _rows; List shows the number of rows per page protected $total _pages; Total pages protected $total _rows; Total number of protected $now _page; Current page protected $method = ' Defalut '; Processing Ajax page HTML paging (static) normal get mode protected $parameter = ';p rotected $page _name; Name of the paging parameter protected $ajax _func_name;public $plus = 3; Paging offset protected $url;/** * constructor * * @param unknown_type $data */public function __construct ($data = Array ()) {$this->t Otal_rows = $data [' total_rows ']; $this->parameter =!empty ($data [' parameter '])? $data [' parameter ']: '; $this->list_rows =!empty ($data [' list_rows ']) && $data [' list_rows '] <= 100? $data [' List_rows ']: $this->total_pages = ceil ($this->total_rows/$this->list_rows) $this->page_name =!empty ($data [' page_name '])? $data [' page_name ']: ' page '; $this->ajax_func_name =!empty ($data [' ajax_func_name '])? $data [' Ajax_func_name ']: '; $this->method =!empty ($data [' method '])? $data [' method ']: ';/* Current Page */if (!empty ($data [' now_page ')) {$this->now_page = intval ($data [' now_page ']);} else {$ This->now_page =!empty ($_get[$this->page_name])? Intval ($_get[$this->page_name]): 1;} $this->now_page = $this->now_page <= 0? 1: $this->now_page;if (!empty ($this->total_pages) && $this->now_page > $this->total_pages) {$ This->now_page = $this->total_pages;} $this->first_row = $this->list_rows * ($this->now_page-1);} /** * Get Current Connection * * @param * $page * @param * $text * @return string */protected function _get_link ($page, $tex T) {switch ($this->method) {case ' ajax ': $parameter = '; if ($this->parameter) {$parameter = ', '. $thisparameter;} Return ' <a onclick= '. $this->ajax_func_name. ' ('. $page. ‘‘‘ . $parameter. ' href= ' javascript:void (0) ">". $text. ' </a> '. ""; break;case ' html ': $url = Str_replace ('? ', $page, $this->parameter); return ' <a href= '. $url. ' > '. $text. ' </a> '. ""; Break;default:return ' <a href= ". $this->_get_url ($page). ' > '. $text. ' </a> '. ""; break;}} /** * Set the current page link */protected function _set_url () {$url = $_server[' Request_uri '). (Strpos ($_server[' Request_uri '), '? ')? ‘‘ : "?") . $this->parameter; $parse = Parse_url ($url), if (Isset ($parse [' query ']) {parse_str ($parse [' query '], $params); unset ($params [$this->page_name]); $url = $parse [' Path ']. ‘?‘ . Http_build_query ($params);} if (!empty ($params)) {$url. = ' & ';} $this->url = $url;} /** * Get $page URL * * @param $page page * @return string */protected function _get_url ($page) {if ($this->url = = = NULL) { $this->_set_url ();} $lable = Strpos (' & ', $this->url) = = = FAThe LSE? ': ' & '; return $this->url. $this->page_name. ' = '. $page;} /** * Get First page * * @return string */public function first_page ($name = ' first page ') {if ($this->now_page > 5) {return $this-& Gt;_get_link (' 1 ', $name);} Return ';} /** * Last Page * * @param * $name * @return string */public function last_page ($name = ' last page ') {if ($this->now_page < $this->total_pages-5) {return $this->_get_link ($this->total_pages, $name);} Return ';} /** * Prev * * @return string */public function up_page ($name = ' previous page ') {if ($this->now_page! = 1) {return $this->_get _link ($this->now_page-1, $name);} Return ';} /** * Next Page * * @return string */public function down_page ($name = ' next page ') {if ($this->now_page < $this->total_pages {return $this->_get_link ($this->now_page + 1, $name);} Return ';}  /** * Paging Style Output * * @param * $param * @return string */public function Show ($param = 1) {if ($this->total_rows < 1) {return ';} $className = ' Show_ '. $param; $classNames = Get_class_methods ($this), if (In_array ($className, $classNames)) {return $this-$className (); Return ';} protected function Show_2 () {if ($this->total_pages! = 1) {$return = "; $return. = $this->up_page (' < '); for ($i = 1; $i <= $this->total_pages;  $i + +) {if ($i = = $this->now_page) {$return. = "<a class= ' now_page ' > $i </a>";} else {if ($this->now_page-  $i >= 4 && $i! = 1) {$return. = "<span class= ' Pagemore ' >...</span>"; $i = $this->now_page-3;} else {if ($i >= $this->now_page + 5 && $i! = $this->total_pages) {$return. = "&LT;SPAN&GT;...&LT;/SPAN&G t; "; $i = $this->total_pages;} $return. = $this->_get_link ($i, $i). "";}}} $return. = $this->down_page (' > '); return $return;}} protected function Show_1 () {$plus = $this->plus;if ($plus + $this->now_page > $this->total_pages) {$begin = $ this->total_pages-$plus * 2;} else {$begin = $this->now_page-$plus;} $begin = ($begin >= 1)?$begin: 1; $return = "; $return. = $this->first_page (); $return. = $this->up_page (); for ($i = $begin; $i <= $begin + $plus * 2; $i + +) {if ($i > $this->total_pages) {break;} if ($i = = $this->now_page) {$return. = "<a class= ' now_page ' > $i </a>";} else {$return. = $this->_get_link ($i, $i). "";}} $return. = $this->down_page (); $return. = $this->last_page (); return $return;} protected function Show_3 () {$plus = $this->plus;if ($plus + $this->now_page > $this->total_pages) {$begin = $ this->total_pages-$plus * 2;} else {$begin = $this->now_page-$plus;} $begin = ($begin >= 1)? $begin: 1; $return = ' total '. $this->total_rows. ' A record is divided into '. $this->total_pages. ' Page, current section '. $this->now_page. ' page '; $return. = ', per page '; $return. = ' <input type= "text" value= ". $this->list_rows. ' "id=" PageSize "size=" 3 ">"; $return. = $this->first_page (). ""; $return. = $this->up_page (). ""; $return. = $this->down_page (). ""; $return.= $this->last_page (). ""; $return. = ' <select onchange= "'. $this->ajax_func_name. ' (this.value) "id=" GotoPage ">" for ($i = $begin; $i <= $begin + $i + +) {if ($i > $this->total_pages) {break ;} if ($i = = $this->now_page) {$return. = ' <option selected= "true" value= "'. $i. ' > '. $i. ' </option> ';} else {$return. = ' <option value= "'. $i. ' > '. $i. ' </option> ';}} $return. = ' </select> '; return $return;}}? >


5. How to use

# # # #处理html静态化页面分页的情况 # # # # html# parameter is the static page parameter xxx.com/20-0-0-0-40 the process environment is set to-?. HTML note Question mark #? The position of the question mark is automatically replaced with the Whereabouts page # Now_page the current page (the static page gets not the current page so only you pass in) $params = Array (' Total_rows ' =>100, # (Must) ' method ' =&G t; ' HTML ', # (MUST) ' parameter ' = ' xxx.com/20-0-0-0-40-?.   HTML ', # (MUST) ' Now_page ' =>$_get[' P '], # (MUST) ' List_rows ' =>10, # (optional) by default); $page = new Pager ($params);   Echo $page->show (1); #<a href= "xxx.com/20-0-0-0-40-2.html" >2</a>## #处理ajax分页的情况 # # Method processing environment set to ajax# Ajax_func_name Ajax Paging Jump page JavaScript method # parameter Ajax_func_name followed by default is empty # Now_page not to the current page so only you pass in $params = Array (' total_rows ' = >100, ' method ' = ' ajax ', ' ajax_func_name ' = ' goToPage ', ' now_page ' =>1,# ' parameter ' = ' ' Jiong ', '   Username ' ",); $page = new Pager ($params); Echo $page->show (1), #<a href= "javascript:void (0)" onclick= "GoToPage (' 7 ')" >7</a> #添加了parameter <a href= "javascript:void (0)" onclick= "GoToPage (' 6 ', ' Jiong ', ' username ')" >6</a>

PHP Universal Paging (Pager) class

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.