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 function, which can implement two forms of text paging and digital paging. It is very practical. If you need it, you can refer

This article is mainly to share with you a piece of PHP encapsulated paging function, which can implement two forms of text paging and digital paging. It is very practical. If you need it, you can refer

Recently, 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'
  • Previous Page |
  • ';
    }
    Else
    {
    // $ _ SERVER ["SCRIPT_NAME"] Get the name of the current script for ease of transplantation
    // You can also customize constants. The constant value is consistent with the script file name.
    Echo'
  • Homepage |
  • ';
    Echo'
  • Previous Page |
  • ';
    }
    // Last page
    If ($ page = $ pageTotal)
    {
    Echo'
  • Next page |
  • ';
    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.