Share a common PHP paging class. There are three kinds of manifestation, the concrete effect chart is as follows:
(1)
(2)
(3)
The specific code for the page class of PHP and how to use it are as follows:
/** *php 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 the number of start rows public $list _rows;//list shows rows per page protected $total _pages;//Total page
Number protected $total _rows;//total number of rows protected $now _page;//Current page protected $method = "Defalut";/handling situation AJAX paging HTML paging (static) normal get way
protected $parameter = "";
Protected $page _name;//The 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->total_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"]:15; $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, $text) {switch ($this->method) {case "Ajax": $parameter = ""; if ($this->parameter) {$parameter = ",". $this->parameter; Return "<a 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 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) = = FALSE? ": &"; RetUrn $this->url. $this->page_name. "
= ". $page; /** * Get the first page * * @return String */Public Function first_page ($name = "First page") {if ($this->now_page>5) {retur
n $this->_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 ""; /** * Previous Page * * @return string/Public Function up_page ($name = "Prev") {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 ""; /** * Pagination 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 &L
T;/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. = "<span>...</span
> ";
$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 paragraph". $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 +10; $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; }
}
class use example:
# # #处理html静态化页面分页的情况 ### # Method processing environment set to HTML # parameter for static page parameters www. Alixixi.com/20-0-0-0-40-?. HTML Note Question mark # The position of the question mark is automatically replaced with the go to page number # Now_page current page (static page gets less than current page so only you pass in) $params =array ("Total_rows" =>100,# (Must) "method" => "HTML", # (MUST) "parameter" => "www." Alixixi.com/20-0-0-0-40-?.
HTML ", # (MUST)" Now_page "=>$_get[" P "], # (MUST)" List_rows "=>10,# (optional) defaults to 15);
$page =new Pager ($params);
Echo $page->show (1); #<a href= "www. Alixixi.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 trailing parameters default to empty # Now_page not to the current page so only you passed in the $params =array ("total_rows" = >100, "method" => "Ajax", "Ajax_func_name" => "GoToPage", "Now_page" =>1, # "parameter" "=>" "Ji
Ong "," username "",);
$page =new Pager ($params);
Echo $page->show (1); #<a href= "javascript:void (0)" >7</a> #添加了parameter <a href= "javascript:void (0)" >6</a>
Articles that you may be interested in
- Summary of methods for using Curl post submission data and get access to Web page data in PHP
- PHP Analysis file header information to determine the type of upload file
- Common methods for judging variable types in PHP
- PHP uses Curl functions to implement multi-threaded crawl Web pages and download files
- PHP control Request Page Browser cache
- PHP determines browser type
- PHP Curl Batch multi-threaded open URL class
- A simple example of PHP getting Web content via socket