Pagination code for php automatic adaptation range

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php
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 = '<div class = "page"> ';
$ Pagecode. = "<span> $ page/$ pages </span>"; // page
// If the first page is displayed, the connection between the first page and the previous page is not displayed.
If ($ page! = 1 ){
$ Pagecode. = "<a href = \" {$ phpfile }? Page = 1 \ "> </a>"; // The first page
$ Pagecode. = "<a href = \" {$ phpfile }? Page = ". ($ page-1)." \ "> </a>"; // 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. = '<span>'. $ I. '</span> ';
} Else {
$ Pagecode. = "<a href = \" {$ phpfile }? Page = {$ I }\ "> $ I </a> ";
}
}
If ($ page! = $ Pages ){
$ Pagecode. = "<a href = \" {$ phpfile }? Page = ". ($ page + 1)." \ ">></a>"; // next page
$ Pagecode. = "<a href = \" {$ phpfile }? Page = {$ pages} \ ">></a>"; // The last page
}
$ Pagecode. = "<input type = \" text \ "size = \" 3 \ "onkeydown = \" if (event. keyCode = 13) {window. location. href = '{$ phpfile }? Page = '+ this. value; return false ;}\ "/> </div> ";
Return array ('pagecode' => $ pagecode, 'sqllimit' => 'limit'. $ offset. ','. $ pagesize );
}
?>

Page number jump text box added
The following is a new user guide
Copy codeThe Code is as follows:
<? Php
$ 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;
}
?>
<? Php
Echo $ getpageinfo ['pagecode']; // display the html code of the page
?>

======================================
Subsidize css
Copy codeThe Code is as follows:
<Style type = "text/css">
Body {font-family: Tahoma ;}
. Page {padding: 2px; font-weight: bolder; font-size: 12px ;}
. Page a {border: 1px solid # ccc; padding: 0 5px 0 5px; margin: 2px; text-decoration: none; color: #333 ;}
. Page span {padding: 0 5px 0 5px; margin: 2px; background: # 09f; color: # fff; border: 1px solid # 09c ;}
</Style>

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.