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

Source: Internet
Author: User

PHP encapsulated paging functions for 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.

Sub-page sub-assembly
/**
* $pageType Paging Type 1 is the number page 2 is the text paging
* $pagetotal, $page, $total and other data can be passed as parameters, or in paging as global variables (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 '
    • A total of '. $total. ' A Member |
    • ';
      First page
      if ($page = = 1)
      {
      Echo '
    • Home |
    • ';
      Echo '
    • Prev |
    • ';
      }
      Else
      {
      $_server["Script_name"] gets the current script name for easy porting
      You can also customize constants, constant values and script filenames are consistent
      Echo '
    • Home |
    • ';
      Echo '
    • Prev |
    • ';
      }
      Last page
      if ($page = = $pageTotal)
      {
      Echo '
    • Next Page |
    • ';
      Echo '
    • End |
    • ';
      }
      Else
      {
      Echo '
    • Next Page |
    • ';
      Echo '
    • End |
    • ';
      }
      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

Split-page parameter loading
/**
* $sql An SQL statement that can get the total number of data
* $size show the number of bars per page
*/
function Pageparam ($sql, $size)
{
Set the global variables for all the parameters involved
$pagestart where a page starts
$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
if (Isset ($_get[' page '))
{
A specific page
$page = $_get[' page '];
Determines 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); Rounding to prevent decimals from appearing
}

}
Else
{
Initialization shows page 1th
$page = 1;
}

Database zeroing
if ($total = = 0)
{
Set to 1
$pageTotal = 1;
}
Else
{
The total number of pages in the page (in-one rounding)
$pageTotal = Ceil ($total/$pagesize);
}

Pages larger than total page $total
if ($page > $pageTotal)
{
$page = $pageTotal;
}
When a 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 to get the total number of data
* The second each page shows the number of bars
*/
Pageparam ("Select userid from User", 2);

Paging Type 1 is a number page 2 is a text paging
Paging (2);
?>

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


Paging Type 1 is a number page 2 is a text paging
Paging (1);
?>

The numbers are paginated as follows:

Style adjusts itself.




http://www.bkjia.com/PHPjc/898294.html www.bkjia.com true http://www.bkjia.com/PHPjc/898294.html techarticle PHP encapsulates paging functions for text paging and digital paging, and most recently, paging is used in the project. Paging is a feature that is often used, so it is sealed in the form of 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.