Php paging code instance (adaptive range)
-
/*
- * Mysql paging function code
- * Edit: bbs.it-home.org
- *
- */
- 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 );
- }
- ?>
2. added a page jump text box.
- $ 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
- ?>
3. the css style table file used in the paging query code.
Recommended reading:
- Php and ajax without refreshing paging code
- Php article paging implementation code
- Php limit paging code
- Php paging class with multiple paging methods
- Php paging code for the previous and next pages
- Top 10 pages and top 10 pages of php paging code
- Example of simple php paging code
- A good php paging code
- One paging function: next page on the previous page
- A handy php paging class
- Php long article paging code
- A Practical php paging class
- Quick php paging
|