The page number paging program automatically adapts to the range, that is, the page number format is as follows: 12345676789101112? Php/** author: php100.com Forum nicholas_ng */functionpage ($ page, $ total, $ phpfile, $ pagesize = 3, $ pagelen =
'123') this. width = '000000'; if (this. offsetHeight> '20140901') this. height = '000000'; "border =" 0 "alt =" \ "/>
Page number paging program that automatically adapts to the range
That is to say, the page number format is as follows:
<1 2 3 4 5 6 7>
<6 7 8 9 10 11 12>
/*
* Author: php100.com Forum Nicolas las_ng
*/
Function page ($ page, $ total, $ phpfile, $ pagesize = 3, $ pagelen = 3 ){
$ Pagecode = "'; // defines variables and stores HTML generated by page
$ Page = intval ($ page); // avoid non-numeric page numbers
$ Total = intval ($ total); // ensure that the total record value type is correct
If (! $ Total) return array (); // The total number of records is zero. an empty array is returned.
$ Pages = ceil ($ total/$ pagesize); // calculates the total page size.
// Process page validity
If ($ page <1) $ page = 1;
If ($ page> $ pages) $ page = $ pages;
// Calculate the query offset
$ Offset = $ pagesize * ($ page-1 );
// Page number range calculation
$ Init = 1; // number of start page codes
$ Max = $ pages; // Number of ending pages
$ Pagelen = ($ pagelen % 2 )? $ Pagelen: $ pagelen + 1; // Number of page numbers
$ Pageoffset = ($ pagelen-1)/2; // page number offset between left and right
// Generate html
$ Pagecode = '';
$ Pagecode. = "$ page/$ pages"; // page
// If the first page is displayed, the connection between the first page and the previous page is not displayed.
If ($ page! = 1 ){
$ Pagecode. = "<"; // page 1
$ Pagecode. = "<"; // Previous Page
}
// Offset when the number of pages is greater than the number of pages
If ($ pages> $ pagelen ){
// If the current page is less than or equal to the left offset
If ($ page <= $ pageoffset ){
$ Init = 1;
$ Max = $ pagelen;
} Else {// if the current page is greater than the left offset
// If the right offset of the current page number exceeds the maximum page number
If ($ page + $ pageoffset >=$ pages + 1 ){
$ Init = $ pages-$ pagelen + 1;
} Else {
// Calculation when both the left and right offsets exist
$ Init = $ page-$ pageoffset;
$ Max = $ page + $ pageoffset;
}
}
}
// Generate html
For ($ I = $ init; $ I <= $ max; $ I ++ ){
If ($ I = $ page ){
$ Pagecode. = ''. $ I .'';
} Else {
$ Pagecode. = "$ I ";
}
}
If ($ page! = $ Pages ){
$ Pagecode. = ">"; // Next page
$ Pagecode. = ">"; // Last Page
}
$ Pagecode. = '';
Return array ('pagecode' => $ pagecode, 'sqllimit' => 'limit'. $ offset. ','. $ pagesize );
}
?>
=========== PHP100 provided ======= demo ============================
$ Phpfile = 'index. php'; // page file name
$ Page = isset ($ _ GET ['Page'])? $ _ GET ['Page']: 1; // Default page number
$ Db = mysql_connect ('localhost', 'test', 'test'); // link to the database
Mysql_select_db ('test', $ db); // select a database
$ Counts = mysql_num_rows (mysql_query ('select' ID' from 'test', $ db); // obtain the total number of required data records
$ SQL = 'SELECT 'id', 'title' from 'test'; // defines the query statement SQL
$ Getpageinfo = page ($ page, $ counts, $ phpfile); // call a function to generate paging HTML and SQL LIMIT clauses
$ SQL. = $ getpageinfo ['sqllimit']; // combine the complete SQL statement
$ Data = $ row = array (); // Initialize an array
$ Result = mysql_query ($ SQL, $ db); // obtain the result set
// Load the data into the $ data array
While ($ row = mysql_fetch_array ($ result )){
$ Data [] = $ row;
}
?>
Echo $ getpageinfo ['pagecode']; // display the html code of the page
?>