Php paging code

Source: Internet
Author: User
A very exquisite php paging class that can be directly applied to the paging system is a necessary tool for mysql database paging, you only need to embed class files and paging CSS styles to achieve exquisite paging. This is a simple, convenient, and fully functional paging class. you can change the CSS style file as needed to control the page color. using the php paging class, you can save yourself a lot of time, you only need to embed it in the paging area. The following describes how to use it:

1. wrap pager.css in the headers

    
 

2. instantiate the class on the page:

 GetPagerContent();      //echo $pageStr;         $myPage=new pager(90,intval($CurrentPage));          $pageStr= $myPage->GetPagerContent();      echo $pageStr;        ?>

3. read mysql databases and paging computing files by yourself. For example:

$ Info = mysql_query ("select * from member order by id desc limit $ offset, $ info_num"); in this way, it can be transferred to the mysql database and the current page number.

The following are a total of three files: (1) index. php: Call the paging class, display the page (2) pager. class. php: php paging class (3366pager.css: css style beautification file

(1) index. php:

    
     ---- Paging demonstration -----    
                 
 GetPagerContent (); // echo $ pageStr; $ myPage = new pager (90, intval ($ CurrentPage); $ pageStr = $ myPage-> GetPagerContent (); echo $ pageStr;?>    

(2) pager. class. php:

 GetPagerContent (); echo $ pageStr; */class Pager {private $ pageSize = 10; private $ pageIndex; private $ totalNum; private $ totalPagesCount; private $ pageUrl; private static $ _ instance; public function _ construct ($ p_totalNum, $ p_pageIndex, $ p_pageSize = 10, $ p_initNum = 3, $ p_initMaxNum = 5) {if (! Isset ($ p_totalNum) |! Isset ($ p_pageIndex) {die ("pager initial error");} $ this-> totalNum = $ p_totalNum; $ this-> pageIndex = $ p_pageIndex; $ this-> pageSize = $ p_pageSize; $ this-> initNum = $ p_initNum; $ this-> initMaxNum = $ p_initMaxNum; $ this-> totalPagesCount = ceil ($ p_totalNum/$ p_pageSize); $ this-> pageUrl = $ this-> _ getPageUrl (); $ this-> _ initPagerLegal ();} /*** get the current URL String for page removal ** @ return String URL String */private funct Ion _ getPageUrl () {$ CurrentUrl = $ _ SERVER ["REQUEST_URI"]; $ arrUrl = parse_url ($ CurrentUrl); $ urlQuery = $ arrUrl ["query"]; if ($ urlQuery) {$ urlQuery = ereg_replace ("(^ | &) page = ". $ this-> pageIndex, "", $ urlQuery); $ CurrentUrl = str_replace ($ arrUrl ["query"], $ urlQuery, $ CurrentUrl); if ($ urlQuery) {$ CurrentUrl. = "& page";} else $ CurrentUrl. = "page";} else {$ CurrentUrl. = "? Page ";} return $ CurrentUrl;}/** set the validity of page parameters * @ return void */private function _ initPagerLegal () {if ((! Is_numeric ($ this-> pageIndex) | $ this-> pageIndex <1) {$ this-> pageIndex = 1 ;} elseif ($ this-> pageIndex> $ this-> totalPagesCount) {$ this-> pageIndex = $ this-> totalPagesCount ;}} // $ this-> pageUrl }={$ I} // {$ this-> CurrentUrl }={$ this-> TotalPages} public function GetPagerContent () {$ str ="

"; // Homepage previous page if ($ this-> pageIndex = 1) {$ str. = "homepage ". "\ n"; $ str. = "previous page ". "\ n ". "\ n";} else {$ str. = "pageUrl} = 1 'class = 'tids' title = 'homepage'> homepage ". "\ n"; $ str. = "pageUrl} = ". ($ this-> pageIndex-1 ). "'class = 'tids' title = 'preput'> Previous Page ". "\ n ". "\ n";}/* page paging logic after the first and last pages * // $ currnt = "" under 10 pages (inclusive ""; if ($ this-> totalPagesCount <= 10) {for ($ I = 1; $ I <= $ this-> totalPagesCount; $ I ++) {if ($ I ==$ this-> pageIndex) {$ currnt = "class = 'current '";} Else {$ currnt =" ";}$ str. = "pageUrl }={$ I} '{$ currnt}> $ I ". "\ n" ;}} else // more than 10 pages {if ($ this-> pageIndex <3) // The current page is less than 3 {for ($ I = 1; $ I <= 3; $ I ++) {if ($ I ==$ this-> pageIndex) {$ currnt = "class = 'current '";} else {$ currnt = "";} $ str. = "pageUrl }={$ I} '{$ currnt}> $ I ". "\ n" ;}$ str. = "...... ". "\ N"; for ($ I = $ this-> totalPagesCount-3 + 1; $ I <= $ this-> totalPagesCount; $ I ++) // Function 1 {$ str. = "pageUrl }={$ I} '> $ I ". "\ n" ;}} elseif ($ this-> pageIndex <= 5) // 5 >=current page >=3 {for ($ I = 1; $ I <= ($ this-> pageIndex + 1); $ I ++) {if ($ I = $ this-> pageIndex) {$ currnt = "class = 'current'";} else {$ currnt = "" ;}$ str. = "pageUrl }={$ I} '{$ currnt}> $ I ". "\ n" ;}$ str. = "...... ". "\ N"; for ($ I = $ this-> totalPagesCount-3 + 1; $ I <= $ this-> totalPagesCount; $ I ++) // Function 1 {$ str. = "pageUrl }={$ I} '> $ I ". "\ n" ;}} elseif (5 <$ this-> pageIndex & $ this-> pageIndex <= $ this-> totalPagesCount-5) // The current page is greater than 5, simultaneously less than the total number of pages-5 {for ($ I = 1; $ I <= 3; $ I ++) {$ str. = "pageUrl }={$ I} '> $ I ". "\ n" ;}$ str. = "...... "; For ($ I = $ this-> pageIndex-1; $ I <= $ this-> pageIndex + 1 & $ I <= $ this-> totalPagesCount-5 + 1; $ I ++) {if ($ I ==$ this-> pageIndex) {$ currnt = "class = 'stream'" ;}else {$ currnt = "";} $ str. = "pageUrl }={$ I} '{$ currnt}> $ I ". "\ n" ;}$ str. = "...... "; For ($ I = $ this-> totalPagesCount-3 + 1; $ I <= $ this-> totalPagesCount; $ I ++) {$ str. = "pageUrl }={$ I} '> $ I ". "\ n" ;}} else {for ($ I = 1; $ I <= 3; $ I ++) {$ str. = "pageUrl }={$ I} '> $ I ". "\ n" ;}$ str. = "...... ". "\ N"; for ($ I = $ this-> totalPagesCount-5; $ I <= $ this-> totalPagesCount; $ I ++) // Function 1 {if ($ I ==$ this-> pageIndex) {$ currnt = "class = 'current'" ;}else {$ currnt = "";} $ str. = "pageUrl }={$ I} '{$ currnt}> $ I ". "\ n" ;}}/ * ends the page Logic after the first and last pages * // if ($ this-> pageIndex ==$ this-> totalPagesCount) {$ str. = "\ n ". "next page ". "\ n"; $ str. = "Last Page ". "\ n";} else {$ str. = "\ n ". "pageUrl} = ". ($ this-> pageIndex + 1 ). "'class = 'tids' title = 'next page'> next page ". "\ n"; $ str. = "pageUrl }={$ this-> totalPagesCount} 'class = 'tids' title = 'Last page'> last page ". "\ n" ;}$ str. ="

"; Return $ str;}/*** get the instance * @ return * // static public function getInstance () {// if (is_null (self :: $ _ instance) {// self: $ _ instance = new pager (); //}/return self: $ _ instance; //}?>

(3) pager.css:

Body, html {padding: 0px; margin: 0px; color: #333333; font-family: "", Arial, Lucida, Verdana, Helvetica, sans-serif; font-size: 12px; line-height: 150%;} h1, h2, h3, h4, h5, h6, ul, li, dl, dt, dd, form, img, p, label {margin: 0; padding: 0; border: none; list-style-type: none;}/** front-end paging style **/. pagination {margin: 10px 0 0; padding: 5px 0; text-align: rightright; height: 20px; line-height: 20px; font-family: Arial, Helvetica, sans-serif, "";}. pagination a {margin-left: 2px; padding: 2px 7px 2px ;}. pagination. dot {border: medium none; padding: 4px 8px }. pagination a: link ,. pagination a: visited {border: 1px solid # dedede; color: #696969; text-decoration: none ;}. pagination a: hover ,. pagination a: active ,. pagination. current: link ,. pagination. current: visited {border: 1px solid # dedede; color: # fff; background-color: # ff6600; background-image: none; border: # ff6600 solid 1px ;}. pagination. selectBar {border: # dedede solid 1px; font-size: 12px; width: 95px; height: 21px; line-height: 21px; margin-left: 10px; display: inline }. pagination. tips {_ padding: 4px 7px 1px ;}

For more articles about php paging code, refer to the Chinese PHP website!

Related articles:

Use PHP to implement simple paging classes and detailed usage

Php paging PHP paging display production details

Php paging principles

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.