Practical and simple PHP paging set includes usage _ PHP Tutorial

Source: Internet
Author: User
Practical and simple PHP paging collection includes usage methods. Method 1: Copy the code as follows :? Php * paging class is used to display version: 1.0 Date: 2013-10-20 ** on multiple data records by page. it is very convenient to call. connect to the database first. Method 1:

The code is as follows:


/*
The paging class is used to display multiple data entries by page.
Version: 1.0
Date: 2013-10-20
*/

/*
The call is very convenient. you can connect to the database and directly transmit the SQL string queried by the person. you can also specify the number of data entries displayed on each page.
For example, $ pages = new Page ('select * FROM 'zy _ common_member '');
Or $ pages = new Page ('select * FROM 'zy _ common_member '', 10 );
*/
Class Page {
Private $ curPage;
Private $ totalPages; // The total number of data pages displayed.
Private $ dispNum; // number of data entries per page
Private $ queryStr; // The queried SQL statement
Private $ limitStr; // The limit control statement after the query statement

/*
Constructor
$ QueryStr SQL statement for data query
$ DispNum number of data entries per page
*/
Public function _ construct ($ queryStr = '', $ dispNum = 10 ){
$ Result = mysql_query ($ queryStr );
$ TotalNum = mysql_num_rows ($ result );
$ This-> dispNum = $ dispNum;
$ This-> totalPages = ceil ($ totalNum/$ dispNum );
$ This-> queryStr = $ queryStr;

$ Temp = (isset ($ _ GET ["curPage"])? $ _ GET ["curPage"]: 1 );
$ This-> setCurPage ($ temp );

$ This-> showCurPage ();
$ This-> showFoot ();
}

/* Display the data content on the current page */
Private function showCurPage (){
$ This-> limitStr = 'limit'. ($ this-> curPage-1) * $ this-> dispNum). ','. $ this-> dispNum;
// Echo $ this-> queryStr. $ this-> limitStr;
$ Result = mysql_query ($ this-> queryStr. $ this-> limitStr );

If (! $ Result)
{
If ($ this-> totalPages> 0)
{
Echo 'query error '.'
';
}
Else
{
Echo 'no data '.'
';
}
Return;
}
$ Cols = mysql_num_fields ($ result );

Echo'



















';Echo' ';For ($ I = 0; $ I <$ cols; $ I ++){Echo' ';}Echo' ';While ($ row = mysql_fetch_assoc ($ result )){Echo' ';Foreach ($ row as $ key => $ value){Echo' ';}Echo' ';}Echo'
';
Echo mysql_field_name ($ result, $ I );
Echo'
';
Echo $ value;
Echo'
';
}

Private function setCurPage ($ curPage ){
If ($ curPage <1)
{
$ CurPage = 1;
}
Else if ($ curPage> $ this-> totalPages)
{
$ CurPage = $ this-> totalPages;
}
$ This-> curPage = $ curPage;
}

/*
Display Page footer information
Such as the home page, previous page, next page, and last page.
*/
Private function showFoot (){
Echo 'homepage ';
Echo 'curpage-1). '"> Previous Page ';
Echo 'curpage + 1). '"> Next Page ';
Echo 'totalpages. '"> last page ';
}

}

?>


Method 2:

The code is as follows:


Class mysqlPager {
Var $ pagePerNum = 5; // The number of data items displayed on each page
Var $ pagePerGroup = 5; // Number of pages in each paging Group
Var $ curPage = 0; // current page, Defualt first page
Var $ totalPage = 0; // total number of pages
Var $ totalNum = 0; // total number of data items
Var $ curPageGroup = 0; // The current paging Group
Var $ curPageUrl = ""; // The URL of the currently used page
Var $ customStyle = ""; // custom style
Var $ pageQuerySql = "";
Function mysqlPager () {// Constructor PHP4
}
/**
* Initialize all variables
*/
Function InitAllVar ($ totalNum, $ pagePerGroup, $ curPageUrl, $ curPage = 1, $ curPageGroup = 1)
{
$ This-> totalNum = $ totalNum;
$ This-> pagePerGroup = $ pagePerGroup;
$ This-> curPageUrl = $ curPageUrl;
$ This-> curPage = $ curPage;
$ This-> curPageGroup = $ curPageGroup;
}
/**
* Set the current page variable
*
* @ Param number $ curPage
*/
Function setCurPage ($ curPage)
{
$ This-> curPage = $ curPage;
}
/**
* Set the current paging group variable
*
* @ Param mixed $ curPageGroup
*/
Function setCurPageGroup ($ curPageGroup)
{
$ This-> curPageGroup = $ curPageGroup;
}
/**
* Set the URL of the distribution class currently used
* $ CurPageUrl string
*/
Function setCurPageUrl ($ curPageUrl)
{
$ This-> curPageUrl = $ curPageUrl;
}
/**
* Get all
*
* @ Param number $ totalNum
* @ Param number $ curPage
* @ Return float
*/
Function getTotalPage ($ totalNum, $ curPage = 0)
{
Return $ this-> totalPage = ceil ($ totalNum/$ this-> pagePerNum );
}
/**
* Set user-defined styles
*
* @ Param mixed $ customStyle
*/
Function setCustomStyle ($ customStyle)
{
$ This-> customStyle = $ customStyle;
}
/**
* Set the user-defined style to return strings
*
*
* @ Param mixed $ pagerString
*/
Function setCustomStyleString ($ pagerString)
{
Return $ styleString = "". $ pagerString ."";
}
/**
* Parameters are not required for output navigation page information, but you must set the corresponding variables before use.
*
* @ Param mixed $ curPageGroup
* @ Param mixed $ curPage
* @ Param mixed $ curPageUrl
*/
Function showNavPager ($ curPageGroup = 0, $ curPage = 0, $ curPageUrl = 0)
{
If ($ curPageGroup)
{
$ This-> curPageGroup = $ curPageGroup;
}
If ($ curPage)
{
$ This-> curPage = $ curPage;
}
If ($ curPageUrl)
{
$ This-> curPageUrl = $ curPageUrl;
}
$ RtnString = "";
// Determine whether the variable is initialized
If ($ this-> curPageGroup & $ this-> curPageUrl & $ this-> totalNum & $ this-> curPage)
{
$ This-> totalPage = $ this-> getTotalPage ($ this-> totalNum );
If ($ this-> curPage = 1)
$ This-> curPage = ($ this-> curPageGroup-1) * $ this-> pagePerGroup + 1;
If ($ this-> curPageGroup! = 1)
{
$ PrePageGroup = $ this-> curPageGroup-1;
$ RtnString. = "curPageUrl ."? CPP = $ prePageGroup> ". $ this-> setCustomStyleString (" <")."";
}
For ($ I = 1; $ I <= $ this-> pagePerGroup; $ I ++)
{
$ CurPageNum = ($ this-> curPageGroup-1) * $ this-> pagePerGroup + $ I;
If ($ curPageNum <= $ this-> totalPage ){
If ($ curPageNum ==$ this-> curPage)
{
$ RtnString. = "". $ this-> setCustomStyleString ($ curPageNum );
} Else
{
$ RtnString. = "curPageUrl? PKI ={$ this-> curPageGroup} & cp = $ curPageNum> ";
$ RtnString. = $ this-> setCustomStyleString ($ curPageNum )."";
}
}
}
If ($ this-> curPageGroup TotalPage/$ this-> pagePerGroup)-1)
{
$ NextPageGroup = $ this-> curPageGroup + 1;
$ RtnString. = "curPageUrl? PKI = $ nextPageGroup> ". $ this-> setCustomStyleString ("> ")."";
}
$ This-> pageQuerySql = "limit". ($ this-> curPage-1) * $ this-> pagePerNum). ",". $ this-> pagePerNum;
}
Else
{
$ RtnString = "error: variable not initialized! ";
}
Return $ rtnString;
}
/**
* Obtain the complete SQL statement for querying MYSQL.
*
* @ Param mixed $ SQL
*/
Function getQuerySqlStr ($ SQL)
{
$ Allsql = $ SQL. $ this-> pageQuerySql;
Return $ allsql;
}
/**
* Set the number of data items on each page
*
* @ Param INT $ num
*/
Function setPagePerNum ($ num)
{
$ This-> pagePerNum = $ num;
}
}
?>


Usage:
$ CurPage = $ _ GET ['CP'];
$ CurPageGroup = $ _ GET ['CP']
If ($ curPage = "")
$ CurPage = 1;
If ($ curPageGroup = "")
$ CurPageGroup = 1;
// Both start from 1. the input data must be verified before, preventing injection.
//...
$ Pager = new MysqlPager ();
$ Pager-> initAllVar (...)
$ Pager-> showNavPager ();
// The subsequent SQL statements can be any output
$ SQL = "select id form dbname ";
$ Querysql = $ pager-> getQuerySqlStr ($ SQL)
// You can query the database with $ querysql to obtain the corresponding result set.


Method 3:

PHP paging functions:

The code is as follows:


<?
// To avoid errors caused by repeated file inclusion,
Added conditions for determining whether a function exists:
If (! Function_exists (pageft )){
// Define the pageft () function. The three parameters are described as follows:
// $ Totle: total information;
// $ Displaypg: number of information displayed on each page. the default value is 20;
// $ Url: link in the paging navigation, except for adding different query information
All parts outside "page" are the same as the URL.
// The default value should be the URL of the current page (that is, $ _ SERVER ["REQUEST_URI"])
But the right side of the default value can only be a constant, so this default value is set to an empty string
In the function, set it to the URL of the current page.
Function pageft ($ totle, $ displaypg = 20, $ url = "){
// Define several global variables:
// $ Page: current page number;
// $ Firstcount: (database) The start item of the query;
// $ Pagenav: page navigation bar code, which is not output in the function;
// $ _ SERVER: required to read the URL "$ _ SERVER [" REQUEST_URI "]" on this page.
Global $ page, $ firstcount, $ pagenav, $ _ SERVER;
// To enable external function access to "$ displaypg ",
Set it as a global variable. Note that a variable is reset.
If it is a global variable, the original value is overwritten, so it is assigned a new value here.
$ GLOBALS ["displaypg"] = $ displaypg;
If (! $ Page) $ page = 1;
// If $ url uses the default value, that is, null value, the value is assigned to the URL of the current page:
If (! $ Url) {$ url = $ _ SERVER ["REQUEST_URI"];}
// URL analysis:
$ Parse_urlparse_url = parse_url ($ url );
$ Url_query = $ parse_url ["query"];
// Retrieve the query string of the URL separately
If ($ url_query ){
// Because the URL may contain page number information, we need to remove it,
To add new page number information.
// The regular expression is used here. See "regular expressions in PHP"
$ Url_query = ereg_replace ("(^ | &) page = $ page", "", $ url_query );

// Replace the query string of the processed URL with the query string of the original URL:
$ Url = str_replace ($ parse_url ["query"], $ url_query, $ url );

// Add page after the URL to query the information, but the value to be assigned is:
If ($ url_query) $ url. = "& page"; else $ url. = "page ";
} Else {
$ Url. = "? Page ";
}
$ Lastpg = ceil ($ totle/$ displaypg );
// The last page, also the total number of pages
$ Page = min ($ lastpg, $ page );
$ Prepg = $ page-1;
// Previous Page
$ Nextpg = ($ page = $ lastpg? 0: $ page + 1 );
// Next page
$ Firstcount = ($ page-1) * $ displaypg;
// Start the paging navigation bar code:
$ Pagenav = "show the <B>". ($ totle? ($ Firstcount + 1): 0 )."
</B>-<B> ". min ($ firstcount + $ displaypg, $ totle )."
</B> a total of $ totle records <BR> ";
// If there is only one page, the function will jump out:
If ($ lastpg <= 1) return false;
$ Pagenav. = "<a href = '$ url = 1'> homepage </a>";
If ($ prepg) $ pagenav. = "<a href = '$ url = $ prepg'>
Previous Page </a> "; else $ pagenav. =" previous page ";
If ($ nextpg) $ pagenav. = "<a href = '$ url = $ nextpg'>
Next page </a> "; else $ pagenav. =" next page ";
$ Pagenav. = "<a href = '$ url = $ lastpg'> last page </a>";
// Pull-down Jump List, listing all page numbers cyclically:
$ Pagenav. = "to the <select name = 'topage'
Size = '1' onchange = 'window. location = \"
$ Url = \ "+ this. value '> \ n ";
For ($ I = 1; $ I <= $ lastpg; $ I ++ ){
If ($ I = $ page) $ pagenav. = "<option value = '$ I'
Selected> $ I </option> \ n ";
Else $ pagenav. = "<option value = '$ I'> $ I </option> \ n ";
}
$ Pagenav. = "</select> Page, total $ lastpg page ";
}
}
?>

Call the pageft () function when paging. However, it does not output anything, but generates several global variables: $ firstcount, $ displaypg, and $ pagenav.
The following is an example of PHP paging function usage:

The code is as follows:


<?
// (Previous program omitted)
Include ("pageft. php ");
// Contains the "pageft. php" file
// Obtain the total number of information
$ Result = mysql_query ("select
* From mytable ");
$ Total = mysql_num_rows ($ result );
// Call pageft (). 10 messages are displayed on each page.
(This parameter can be omitted if the default value is 20 ),
Use the URL of this page (default, so it is omitted ).
Pageft ($ total, 10 );
// The global variables generated now come in handy:
$ Result = mysql_query ("select *
From mytable limit $ firstcount,
$ Displaypg ");
While ($ row = mysql_fetch_array ($ result )){
// (List content omitted)
}
// Output the pagination navigation bar code:
Echo $ pagenav;
// (Subsequent procedures)
?>

The pipeline code is as follows :? Php/* paging class is used to display multiple data records by page version: 1.0 Date: 2013-10-20 * // * the call is very convenient. first, connect to the database directly...

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.