Perfect php paging class, perfect php paging class

Source: Internet
Author: User

Perfect php paging class, perfect php paging class

The examples in this article share the code of the php paging class for your reference. The details are as follows:

<? Php/** file: page. class. php perfect paging Page */class Page {private $ total; // total number of records in the data table private $ listRows; // The number of lines displayed on each Page is private $ limit; // The SQL statement uses the limit clause to limit the number of retrieved records. private $ uri; // The request address for Automatically Obtaining the url is private $ pageNum; // the total number of pages is private $ page; // private $ config = array ('head' => "record", 'prev' => "Previous Page", 'Next' => "next page ", 'first' => "Homepage", 'last' => "last page"); // display the content in the page information. You can use set () method: Set private $ listNum = 10; // default number displayed in the list of pages Number/** constructor. You can set the attribute @ param int $ total of the paging class to calculate the total number of records @ param int $ listRows (optional, set the number of records to be displayed on each page. The default value is 25 @ param mixed $ query (Optional). It is an array used to pass parameters to the target page, it can also be an optional string Query format @ param bool $ ord. The default value is true. The page is displayed from the first page, false indicates the last page */public function _ construct ($ total, $ listRows = 25, $ query = "", $ ord = true) {$ this-> total = $ total; $ this-> listRows = $ listRows; $ this-> uri = $ this-> getUri ($ query ); $ this-> pageNum = ceil ($ this-> total /$ This-> listRows);/* the following judgment is used to set the current surface */if (! Empty ($ _ GET ["page"]) {$ page = $ _ GET ["page"];} else {if ($ ord) $ page = 1; else $ page = $ this-> pageNum;} if ($ total> 0) {if (preg_match ('/\ D/', $ page )) {$ this-> page = 1;} else {$ this-> page = $ page ;}} else {$ this-> page = 0 ;} $ this-> limit = "LIMIT ". $ this-> setLimit ();}/** is used to set the display page information, you can perform a coherent operation @ param string $ param is the subscript of the member attribute array config @ param string $ value is used to set the element value corresponding to the config subscript @ return object to return the current object $ thi S, used for connected operations */function set ($ param, $ value) {if (array_key_exists ($ param, $ this-> config )) {$ this-> config [$ param] = $ value;} return $ this;}/* is not called directly. Through this method, you can directly obtain the limit and page values of Private member attributes outside the object */function _ get ($ args) {if ($ args = "limit" | $ args = "page") return $ this-> $ args; else return null ;} /** output pagination @ param int 0-7 numbers in the specified format as parameters for customizing the output pagination structure and adjusting the structure order, all structure output by default @ return string paging information content */function fpag E () {$ arr = func_get_args (); $ html [0] = "<span class = 'p1'> total <B >{$ this-> total} </B >{$ this-> config [" head" ]} </span> "; $ html [1] = "this page <B> ". $ this-> disnum (). "</B> entries"; $ html [2] = "this page is from <B >{$ this-> start ()}-{$ this-> end ()} </B> entries "; $ html [3] =" <B >{$ this-> page}/{$ this-> pageNum} </B> page "; $ html [4] = $ this-> firstprev (); $ html [5] = $ this-> pageList (); $ html [6] = $ this-> nextlast (); $ html [7] = $ this-> goPage (); $ Fpage = '<div style = "font: 12px \' \ 5B8B \ 4F53 \ ', san-serif;">'; if (count ($ arr) <1) $ arr = array (0, 1, 2, 3, 4, 5, 6, 7); for ($ I = 0; $ I <count ($ arr); $ I ++) $ fpage. = $ html [$ arr [$ I]; $ fpage. = '</div>'; return $ fpage;}/* private method used inside the object, */private function setLimit () {if ($ this-> page> 0) return ($ this-> page-1) * $ this-> listRows. ", {$ this-> listRows}"; else return 0;}/* Private method used inside the object to automatically obtain the current URL */priva for access Te function getUri ($ query) {$ request_uri = $ _ SERVER ["REQUEST_URI"]; $ url = strstr ($ request_uri ,'? ')? $ Request_uri: $ request_uri .'? '; If (is_array ($ query) $ url. = http_build_query ($ query); else if ($ query! = "") $ Url. = "&". trim ($ query ,"? & "); $ Arr = parse_url ($ url); if (isset ($ arr [" query "]) {parse_str ($ arr [" query "], $ arrs); unset ($ arrs ["page"]); $ url = $ arr ["path"]. '? '. Http_build_query ($ arrs);} if (strstr ($ url ,'? ') {If (substr ($ url,-1 )! = '? ') $ Url = $ url.' & ';} else {$ url = $ url .'? ';} Return $ url;}/* private method used inside the object to obtain the number of records starting from the current page */private function start () {if ($ this-> total = 0) return 0; else return ($ this-> page-1) * $ this-> listRows + 1 ;} /* private method used inside the object to obtain the number of records ending on the current page */private function end () {return min ($ this-> page * $ this-> listRows, $ this-> total);}/* Private method used inside the object, used to obtain operation information of the previous page and homepage */private function firstprev () {if ($ this-> page> 1) {$ str = "<a href = '{$ this-> uri} page = 1 '>{$ This-> config ["first"]} </a> "; $ str. = "<a href = '{$ this-> uri} page = ". ($ this-> page-1 ). "'>{$ this-> config [" prev "]} </a>"; return $ str ;}/ * Private method used inside the object, used to obtain page list information */private function pageList () {$ linkPage = "<B>"; $ inum = floor ($ this-> listNum/2 ); /* List before the current page */for ($ I = $ inum; $ I >=1; $ I --) {$ page = $ this-> page-$ I; if ($ page> = 1) $ linkPage. = "<a href = '{$ this-> uri} page = {$ page}' >{$ page} </a> ";} /* Information on the current page */if ($ this-> pageNum> 1) $ linkPage. = "<span style = 'padding: 1px 2px; background: # BBB; color: white' >{$ this-> page} </span> "; /* list after the current page */for ($ I = 1; $ I <= $ inum; $ I ++) {$ page = $ this-> page + $ I; if ($ page <= $ this-> pageNum) $ linkPage. = "<a href = '{$ this-> uri} page = {$ page}' >{$ page} </a>"; else break;} $ linkPage. = '</B>'; return $ linkPage;}/* private method used inside the object to obtain operation information on the next and last pages */private function nex Tlast () {if ($ this-> page! = $ This-> pageNum) {$ str = "<a href = '{$ this-> uri} page = ". ($ this-> page + 1 ). "'>{$ this-> config [" next "]} </a>"; $ str. = "<a href = '{$ this-> uri} page = ". ($ this-> pageNum ). "'>{$ this-> config [" last "]} </a>"; return $ str ;}/ * Private method used inside the object, used to display and process the form jump page */private function goPage () {if ($ this-> pageNum> 1) {return '<input style = "width: 20px; height: 17px! Important; height: 18px; border: 1px solid # CCCCCC; "type =" text "onkeydown =" javascript: if (event. keyCode = 13) {var page = (this. value> '. $ this-> pageNum. ')? '. $ This-> pageNum. ': this. value; location = \''. $ this-> uri. 'page = \ '+ page + \'} "value = "'. $ this-> page. '"> <input style =" cursor: pointer; width: 25px; height: 18px; border: 1px solid # CCCCCC; "type =" button "value =" GO "onclick =" javascript: var page = (this. previussibling. value> '. $ this-> pageNum. ')? '. $ This-> pageNum. ': this. previussibling. value; location = \''. $ this-> uri. 'page = \ '+ page + \' "> ';}}/* Private method used inside the object, used to obtain the number of records displayed on this page */private function disnum () {if ($ this-> total> 0) {return $ this-> end () -$ this-> start () + 1 ;}else {return 0 ;}}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.