PHP functions for paging with text pagination and digital paging, _php tutorial

Source: Internet
Author: User

PHP functions implement paging with text paging and digital paging,

Recently, paging was used in the project. Paging is a feature that is often used, so it is encapsulated in a functional form.

Page split/*** $pageType page Type 1 is the number of page 2 is the text paging * can be $pagetotal, $page, $total and other data as parameters, or in 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 '
  • '; Echo '
  • A total of '. $total. '
  • ';//First page if ($page = = 1) {echo '
  • '; Echo '
  • ';} else{//$_server["Script_name"] gets the current script name, facilitates porting//can also customize constants, constant values and script filenames consistently echo '
  • '; Echo '
  • ';} Last page if ($page = = $pageTotal) {echo '
  • '; Echo '
  • ';} Else{echo '
  • '; Echo '
  • ';} echo
'; Echo ';}

Parameter explanation:

$pageTotal is the total number of pages, $page is the current page, $total is the total amount of data obtained from the database;

For simplicity, all parameters are encapsulated

Paging parameter/*** $sql an SQL statement that can get the total number of data * $size shows the number of bars per page */function pageparam ($sql, $size) {//sets all the parameters involved in global variables//$pagestart Where does a page start//$total total number of records $page a page $pageTotal total pages Global $pagestart, $pagesize, $total, $page, $pageTotal; $pagesize = $size;// Get Total Data $total = mysql_num_rows (Querydb ($sql));//error handling, first determine if there is an if (Isset ($_get[' page ')) {//specific page $page = $_get[' page '];/ /Determines whether it is empty (0 is empty)/less than 0/is a number if (Empty ($page) | | $page < 0 | |!is_numeric ($page)) {$page = 1;} else{$page = intval ($page);//rounding, preventing fractional occurrences}}else{//initialization shows 1th page $page = 1;} Database Clear 0 if ($total = = 0) {//set to 1$pagetotal = 1;} else{//the total number of pages (in-process) $pageTotal = Ceil ($total/$pagesize);} Pages greater than total page $totalif ($page > $pageTotal) {$page = $pageTotal;} When the page starts from a record $pagestart = ($page-1) * $pagesize;}

Parameter explanation:

$pagestart is when a page starts from a record, $pagesize is the number of records displayed per page

In use, call Pageparam First, and then call paging

/*** the first SQL statement that can fetch the total number of data * The second page shows the number of bars */pageparam ("Select userid from User", 2), <?php//paging type 1 is the number page 2 is the text paging paging (2);? >

The location of the call is selected according to the situation, and the text is paginated as follows:

The numbers are paginated as follows:

Style adjusts itself. true techarticle PHP functions implement paging with text pagination and digital paging, and most recently, paging is used in the project. Paging is a feature that is often used, so it is encapsulated in the form of a function ...

  • 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: 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.