PHP function paging includes text paging and digital Paging

Source: Internet
Author: User
Paging is a frequently used function. Therefore, pagination, text pagination, and numeric pagination are implemented using PHP and encapsulated in the form of functions.

Paging is a frequently used function. Therefore, pagination, text pagination, and numeric pagination are implemented using PHP and encapsulated in the form of functions.

Recently, pagination is used in the project. The paging function is often used. Therefore, it is encapsulated as a function.

// Paging/***** $ pageType paging type 1: Digital 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 |
  • '; // The first page if ($ page = 1) {echo'
  • Homepage |
  • '; Echo'
  • Previous Page |
  • ';} Else {// $ _ SERVER ["SCRIPT_NAME"] Get the current Script Name, which facilitates porting // You can also customize constants. The constant value is consistent with the script file name echo'
  • Homepage |
  • '; Echo'
  • Previous Page |
  • ';} // The 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.

// Paging parameter package/*** $ SQL can be used to obtain the total number of data in an SQL statement * $ size the number of entries displayed on each page */function pageParam ($ SQL, $ size) {// set global variables for all involved parameters // $ where to start a page of pagestart // $ total number of records $ page a page $ total pageTotal pages global $ pagestart, $ pagesize, $ total, $ page, $ pageTotal; $ pagesize = $ size; // total number of Retrieved Data $ total = mysql_num_rows (queryDB ($ SQL); // error handling, first, determine whether there is an if (isset ($ _ GET ['page']) {// a 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, prevent decimal occurrence} else {// initialize display 1st page $ page = 1;} // CLEAR database if ($ total = 0) {// set to 1 $ pageTotal = 1;} total number of else {// pages (one-to-one integer) $ pageTotal = ceil ($ total/$ pagesize );} // The page number is greater than the total page number $ totalif ($ 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 * The number of entries displayed on the second page */pageParam ("select userid from user", 2); <? Php // paging type 1: Digital paging 2: Text paging (2);?>

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

<? Php // paging type 1: Digital paging 2: Text paging (1);?>

The number page is as follows:

Adjust the style.

Related Article

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.