PHP function implementation paging includes text paging and digital paging, _ PHP Tutorial

Source: Internet
Author: User
The PHP function supports pagination of text pages and numbers ,. The PHP function supports pagination of text Pages and Numbers. recently, pagination is used in projects. Paging is a frequently used function. Therefore, PHP functions are encapsulated in the form of functions to implement paging including text paging and digital paging,

Recently, pagination is required 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.




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

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.