A simple PHP paging grouping class

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php
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 = "<span class =". $ customStyle. ">". $ pagerString. "</span> ";
}
/**
* 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. = "<a href =". $ this-> curPageUrl ."? CPP = $ prePageGroup> ". $ this-> setCustomStyleString (" <")." </a> ";
}
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. = "<a href = $ this-> curPageUrl? PKI ={$ this-> curPageGroup} & cp = $ curPageNum> ";
$ RtnString. = $ this-> setCustomStyleString ($ curPageNum). "</a> ";
}
}
}
If ($ this-> curPageGroup <ceil ($ this-> totalPage/$ this-> pagePerGroup)-1)
{
$ NextPageGroup = $ this-> curPageGroup + 1;
$ RtnString. = "<a href = $ this-> curPageUrl? PKI = $ nextPageGroup> ". $ this-> setCustomStyleString ("> ")." </a> ";
}
$ 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.

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.