Php automatically adapts to the range of paging code _ PHP Tutorial

Source: Internet
Author: User
Php automatically adapts to the paging code of the range. Copy the code as follows :? Phpfunctionpage ($ page, $ total, $ phpfile, $ pagesize10, $ pagelen7) {$ pagecode; defines variables and stores HTML $ pageintval ($ page) generated by pagination. avoid uncounting The code is as follows:


Function page ($ page, $ total, $ phpfile, $ pagesize = 10, $ pagelen = 7 ){
$ 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 );
}
?>


Page number jump text box added
The following is a new user guide

The code is as follows:


$ 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
?>


======================================
Subsidize css

The code is as follows:



The http://www.bkjia.com/PHPjc/319325.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/319325.htmlTechArticle code is as follows :? Php functionpage ($ page, $ total, $ phpfile, $ pagesize = 10, $ pagelen = 7) {$ pagecode = ''; // defines the variable, store HTML $ page generated by page = intval ($ page); // avoid non-count...

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.