Php paging function sample code sharing
This article mainly introduces the php paging function sample code. For more information, see
Share a php paging function code example. Using this function to implement paging code is good.
Code, php paging function.
The Code is as follows:
<? 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); // The total number of retrieved information.
PageDivide ($ total, 10); // call the paging Function
// Database operations
$ Result = mysql_query ("select * from mytable limit $ sqlfirst, $ shownu", $ myconn );
While ($ row = mysql_fetch_array ($ result )){
... Your operations
}
Echo $ pagecon; // The output page navigation content.
*/
If (! Function_exists ("pageDivide ")){
# $ Total Information
# $ Shownu display quantity. The default value is 20.
# $ Url link to this page
Function pageDivide ($ total, $ shownu = 20, $ url = ''){
# $ Current page number
# $ Sqlfirst mysql database start item
# $ Pagecon paging 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 assigned to the URL of the current page.
If (! $ Url) {$ url = $ _ SERVER ["REQUEST_URI"];}
# URL Analysis
$ Parse_url = parse_url ($ url );
@ $ Url_query = $ parse_url ["query"]; // The question mark? Subsequent 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
$ Page = min ($ lastpg, $ page );
$ Prepg = $ page-1; // Previous page
$ Nextpg = ($ page = $ lastpg? 0: $ page + 1); // next page
$ Sqlfirst = ($ page-1) * $ shownu;
# Start paging navigation content
$ Pagecon = "display Number". ($ total? ($ Sqlfirst + 1): 0 ). "-". min ($ sqlfirst + $ shownu, $ total ). "records, total <B> $ total </B> records ";
If ($ lastpg <= 1) return false; // jumps out if there is only one page
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. = "next page ";
If ($ page! = $ Lastpg) $ pagecon. = "<a href = '$ url = $ lastpg'> last page </a>"; else $ pagecon. = "last page ";
# Pull-down Jump list, listing all page numbers cyclically
$ 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 ";
}
} The else die ('pagedivide () function with the same name already exists! ');
?>