PHP encapsulation paging function for text paging and digital paging

Source: Internet
Author: User
This article is mainly to share with you a piece of PHP encapsulated paging functions, which can be implemented in two forms: text paging and digital paging. it is very practical. if you need it, please refer to the following, pagination is used in the project. The paging function is often used. Therefore, it is encapsulated as a function.

// Paging and sub-assembly
/**
* $ PageType paging Type 1: numeric paging 2: text paging
* You can pass $ pageTotal, $ page, $ total, and other data as parameters, or use paging as a global variable (recommended)
*/
Function paging ($ pageType)
{
Global $ pageTotal, $ page, $ total;
If ($ pageType = 1)
{
Echo'

';
Echo'

    ';
    For ($ I = 0; $ I <$ pageTotal; $ I ++)
    {
    If ($ page = ($ I + 1 ))
    {
    Echo'
  • '. ($ I + 1 ).'
  • ';
    }
    Else
    {
    Echo'
  • '. ($ I + 1 ).'
  • ';
    }
    }
    Echo'
';
Echo'

';

}
Else if ($ pageType = 2)
{
Echo'

';
Echo'

    ';
    Echo'
  • '. $ Page.'/'. $ pageTotal.' page |
  • ';
    Echo'
  • Total'. $ Total .'Members |
  • ';
    // Page 1
    If ($ page = 1)
    {
    Echo'
  • Homepage |
  • ';
    Echo'
  • |
  • ';
    Echo'
  • Last page |
  • ';
    }
    Else
    {
    Echo'
  • Next Page |
  • ';
    Echo'
  • Last page |
  • ';
    }
    Echo'
';
Echo'

';
}
}

Parameter description:

$ PageTotal indicates the total number of pages, $ page indicates the current page, and $ total indicates the total number of data retrieved from the database;

To simplify the process, encapsulate all parameters.

// Split paging parameters
/**
* $ SQL: an SQL statement that obtains the total number of data.
* $ Size number of entries displayed on each page
*/
Function pageParam ($ SQL, $ size)
{
// Set global variables for all involved parameters
// $ Where does pagestart start from?
// $ Total number of records $ page a page $ total number of pageTotal pages
Global $ pagestart, $ pagesize, $ total, $ page, $ pageTotal;
$ Pagesize = $ size;
// Obtain the total number of data items
$ Total = mysql_num_rows (queryDB ($ SQL ));

// Handle the error. First, determine whether the error exists.
If (isset ($ _ GET ['Page'])
{
// Specific page
$ Page = $ _ GET ['Page'];
// Determine whether it is empty (0 is empty)/less than 0/whether it is a number
If (empty ($ page) | $ page <0 |! Is_numeric ($ page ))
{
$ Page = 1;
}
Else
{
$ Page = intval ($ page); // integer to prevent decimal occurrence
}

}
Else
{
// Page 1st is displayed during initialization.
$ Page = 1;
}

// Reset the database
If ($ total = 0)
{
// Set to 1
$ PageTotal = 1;
}
Else
{
// The total number of pages (one-to-one integer)
$ PageTotal = ceil ($ total/$ pagesize );
}

// The page number is greater than the total page number $ total
If ($ page> $ pageTotal)
{
$ Page = $ pageTotal;
}
// When the page starts from a record
$ Pagestart = ($ page-1) * $ pagesize;
}

Parameter description:

$ Pagestart: When a page starts from a record, $ pagesize indicates the number of records displayed on each page.

In use, call pageParam first, and then call paging

/**
* The first SQL statement that can obtain the total number of data
* Number of Entries displayed on the second page
*/
PageParam ("select userid from user", 2 );
<? Php
// Page Type 1: digital page 2: text page
Paging (2 );
?>

Select the call location based on the actual situation. the text page is as follows:

<? Php
// Page Type 1: digital page 2: text page
Paging (1 );
?>

The number page is as follows:

Adjust the 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.