PHP implements paging functions like Google paging

Source: Internet
Author: User
This article mainly introduces the paging function for PHP to implement the Google paging effect. The example analyzes the paging functions related to php implementation, which has some reference value, for more information, see the example in this article to describe the paging function of PHP to implement the Google-like paging effect. Share it with you for your reference. The details are as follows:

/*** Paging function ** @ param int $ total page number * @ param int $ pagesize the number of pages on each page * @ param string $ name of the page variable in the pkey url * @ param string $ basic url, the {page} will be replaced with the page number * dependent function request_uri httpInt */function page ($ total, $ pagesize = 10, $ pkey = 'p', $ url = null) {$ a_num = 10; $ first = 'homepage'; $ last = 'Last page'; $ pre = 'previous page'; $ next = 'next page '; $ a_num = $ a_num % 2 = 0? $ A_num + 1: $ a_num; if (! $ Url) {$ url = preg_replace ("/&? {$ Pkey} = [^ &] {0,}/",'', request_uri (); $ url = strpos ($ url ,'? ') = FALSE? $ Url .'? '. $ Pkey. '= {page}': $ url. '&'. $ pkey. '= {page}';} $ pages = ceil ($ total/$ pagesize); $ curpage = httpInt ($ pkey, false, 1 ); $ curpage = $ curpage> $ pages | $ curpage <= 0? 1: $ curpage; // set the current page to 1 $ body = ''; $ prefix =''; $ subfix = ''; $ start = $ curpage-($ a_num-1)/2; // start Page $ end = $ curpage + ($ a_num-1)/2; // end page $ start = $ start <= 0? 1: $ start; // overrange correction of the start page $ end = $ end> $ pages? $ Pages: $ end; // end page out-of-range correction if ($ pages >=$ a_num) {// total pages greater than display pages if ($ curpage <= ($ a_num-1) /2) {$ end = $ a_num;} // The current page fills the right side in the left half if ($ end-$ curpage <= ($ a_num-1)/2) {$ start-= 5-($ end-$ curpage);} // left side of the current page in the right half} for ($ I = $ start; $ I <= $ end; $ I ++) {if ($ I ==$ curpage) {$ body. =''. $ I .'';} Else {$ body. = ''. $ I.'' ;}}$ prefix = ($ curpage = 1? '':''. $ First. ''. $ pre.''); $ subfix = ($ curpage = $ pages? '':''. $ Next. ''. $ last. ''); $ info =" page {$ curpage}/{$ pages} "; $ go = 'script function ekup () {if (event. keyCode = 13) {clkyup () ;}} function clkyup () {if (! /\ D +/. test (document. getElementById (\ 'gsd09fhas9d \ '). value) {alert (\' enter the page number! \ '); Return;}; location = \''. $ url. '\'. replace (// \ {page \}/, document. getElementById (\ 'gsd09fhas9d \'). value);} scriptGo to '; return $ prefix. $ body. $ subfix. $ info. $ go;}/*** description: a general solution for getting the value of _ SERVER ['request _ URI '] */function request_uri () {if (isset ($ _ SERVER ['request _ URI ']) {$ uri = $ _ SERVER ['request _ URI'];} else {if (isset ($ _ SERVER ['argv']) {$ uri = $ _ SERVER ['php _ SELF ']. '? '. $ _ SERVER ['argv'] [0];} else {$ uri = $ _ SERVER ['php _ SELF']. '? '. $ _ SERVER ['query _ string'] ;}return $ uri ;} /*** get the integer passed by HTTP * @ param string $ key parameter name * @ param bool $ ispost adopts the POST transmission method, which is true by default. http://blog.ddian.cn * @ Return Integer. the default value is $ default */function httpInt ($ key, $ ispost = true, $ default = 0) {if ($ ispost) {if (isset ($ _ POST [$ key]) {if (empty ($ _ POST [$ key]) {return $ default ;} else {$ val = trim ($ _ POST [$ key]); if (preg_match ("/^ \-? \ D + $/", $ val) return intval ($ val); else return $ default ;}} else {return $ default ;}} else {if (isset ($ _ GET [$ key]) {if (empty ($ _ GET [$ key]) {return $ default ;} else {$ val = trim ($ _ GET [$ key]); if (preg_match ("/^ \-? \ D + $/", $ val) {return intval ($ val) ;}else {return $ default ;}} else {return $ default ;}}}

I hope this article will help you with php programming.

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.