Php paging function sample code, php paging code implementation method, paging sample code

Source: Internet
Author: User

Php paging function sample code, php paging code implementation method, paging sample code
Php paging function sample code

Share a php paging function code example. Using this function to implement paging code is good.

Code, php paging function.

<? Php/** Created on 2011-07-28 * Author: LKK, http://lianq.net * usage: require_once ('mypage. php '); $ result = mysql_query ("select * from mytable", $ myconn); $ total = mysql_num_rows ($ result); // pageDivide ($ total, 10); // call the paging function // database operation $ result = mysql_query ("select * from mytable limit $ sqlfirst, $ shownu", $ myconn ); while ($ row = mysql_fetch_array ($ result )){... your operation} echo $ pagecon; // output the paging navigation content */if (! Function_exists ("pageDivide") {# $ total Information # $ shownu display quantity, default 20 # $ url link to this page function pageDivide ($ total, $ shownu = 20, $ url = '') {# $ page Current page number www.jbxue.com # $ sqlfirst mysql database start item # $ pagecon page navigation content global $ page, $ sqlfirst, $ pagecon, $ _ SERVER; $ GLOBALS ["shownu"] = $ shownu; if (isset ($ _ GET ['page']) {$ page = $ _ GET ['page'];} else $ page = 1; # If $ url uses the default value, that is, null value, the value is URLif (! $ Url) {$ url = $ _ SERVER ["REQUEST_URI"] ;}# URL analysis $ parse_url = parse_url ($ url ); @ $ url_query = $ parse_url ["query"]; // The question mark? After the content if ($ url_query) {$ url_query = preg_replace ("/(&?) (Page = $ page)/"," ", $ url_query); $ url = str_replace ($ parse_url [" query "], $ url_query, $ url ); if ($ url_query) {$ url. = "& page";} else $ url. = "page";} else $ url. = "? Page "; # page number calculation $ lastpg = ceil ($ total/$ shownu); // last page, total page number $ page = min ($ lastpg, $ page ); $ prepg = $ page-1; // Previous page $ nextpg = ($ page = $ lastpg? 0: $ page + 1); // next page $ sqlfirst = ($ page-1) * $ shownu; # Start pagecon navigation content $ pagecon = "show no ". ($ total? ($ Sqlfirst + 1): 0 ). "-". min ($ sqlfirst + $ shownu, $ total ). "records, total <B> $ total </B> records"; if ($ lastpg <= 1) return false; // if there is only one page, the if ($ page! = 1) $ pagecon. = "<a href = '$ url = 1'> homepage </a>"; else $ pagecon. = "Homepage"; if ($ prepg) $ pagecon. = "<a href = '$ url = $ prepg'> previous page </a>"; else $ pagecon. = "Previous Page"; if ($ nextpg) $ pagecon. = "<a href = '$ url = $ nextpg'> next page </a>"; else $ pagecon. = "back page"; if ($ page! = $ Lastpg) $ pagecon. = "<a href = '$ url = $ lastpg'> last page </a>"; else $ pagecon. = "last page"; # Jump to the drop-down list and list all pages in a loop $ pagecon. = "to the <select name = 'topage' size = '1' onchange = 'window. location = \ "$ url = \" + this. value'> \ n "; for ($ I = 1; $ I <= $ lastpg; $ I ++) {if ($ I = $ page) $ pagecon. = "<option value = '$ I' selected> $ I </option> \ n"; else $ pagecon. = "<option value = '$ I'> $ I </option> \ n" ;}$ pagecon. = "</select> page, total $ lastpg page" ;}} else die ('pagedivide () The same name function already exists! ');?>

Articles you may be interested in:
Simple paging code for entry-level PHP
Detailed explanation of PHP paging code (with Instance attached)
Php simple paging code example
Php paging class with multiple paging Methods
A handy php paging class
Example of simple php paging code
A Practical php paging class
A quick and easy-to-use php paging class


PHP paging code

Let me give you a class. It's very simple,
<? Php
// Paging Function
Class pg {
Function genpage (& $ SQL, $ page_size = 2)
{
Global $ prepage, $ nextpage, $ pages, $ sums; // out param
$ Page = $ _ GET ["page"];
$ Eachpage = $ page_size;
$ Pagesql = strstr ($ SQL, "from ");
$ Pagesql = "select count (*) as ids". $ pagesql;
$ Result = mysql_query ($ pagesql) or die (mysql_error ());
If ($ rs = mysql_fetch_array ($ result) $ sums = $ rs [0];
$ Pages = ceil ($ sums-0.5)/$ eachpage)-1;
$ Pages = $ pages> = 0? $ Pages: 0;
$ Prepage = ($ page> 0 )? $ Page-1: 0;
$ Nextpage = ($ page <$ pages )? $ Page + 1: $ pages;
$ Startpos = $ page * $ eachpage;
$ SQL. = "limit $ startpos, $ eachpage ";
}
Function showpage ()
{
Global $ page, $ pages, $ prepage, $ nextpage, $ queryString;
$ QueryString = $ _ SERVER ['query _ string'];
If (preg_match ("/page/", $ queryString )){
$ QueryString = strstr ($ queryString ,"&");
} Else {
$ QueryString = "&". $ queryString;
}

$ Shownum = 10/2;
$ Startpage = ($ page >=$ shownum )? $ Page-$ shownum: 0;
$ Endpage = ($ page + $ shownum <= $ pages )? $ Page + $ shownum: $ pages;
$ Xs = "";
$ Xs. = "Total". ($ pages + 1). "Page :";
If ($ page> 0) $ xs. = "<a href = $ PHP_SELF? Page = 0 $ queryString> homepage </a> ";
If ($ startpage> 0)
$ Xs. = "... <B> <a href = $ PHP_SELF? Page = ". ($ page-$ shownum * 2)." $ queryString>? </A>... the remaining full text>

PHP paging code

<? Php
Include ("connection. php ");
$ PerNumber = 10; // number of records displayed on each page
$ Page = $ _ GET ['page']; // obtain the current page value.
$ Count = mysql_query ("select count (*) from user"); // obtain the total number of records
$ Rs = mysql_fetch_array ($ count );
$ TotalNumber = $ rs [0];
$ TotalPage = ceil ($ totalNumber/$ perNumber); // calculate the total number of pages
If (! Isset ($ page )){
$ Page = 1;
} // If no value exists, 1 is assigned.
$ StartCount = ($ page-1) * $ perNumber; // start by page. Calculate the start record based on this method.
$ Result = mysql_query ("select * from user limit $ startCount, $ perNumber"); // calculate the start record and number of records based on the previous calculation.
While ($ row = mysql_fetch_array ($ result )){
Echo "user_id:". $ row [0]. "<br> ";
Echo "username:". $ row [1]. "<br>"; // displays the database content.
}
If ($ page! = 1) {// the page number is not equal to 1
?>
<A href = "fenye. php? Page = <? Php echo $ page-1;?> "> Previous page </a> <! -- Display previous page -->
<? Php
}
For ($ I = 1; $ I <= $ totalPage; $ I ++) {// The page is displayed cyclically.
?>
<A href = "fenye. php? Page = <? Php echo $ I;?> "> <? Php echo $ I;?> </A>
<? Php
}
If ($ page <$ totalPage) {// if the page is smaller than the total number of pages, the next page Link is displayed.
?>
<A href = "fenye. php? Page = <? Php echo $ page + 1;?> "> Next page </a>
<? Php
}
?>
======================================

This is very simple... and I wrote comments... I don't know if it doesn't suit you ..

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.