Php paging extension code

Source: Internet
Author: User

I used to publish a paging class out of the database. Recently, I found that sometimes when the search is too large, I used url to pass parameters, it may have some impact or known bugs. This time, we made some extensions and made them compatible with previous models. The above is very simple to use, the Code is as follows: Copy codeThe Code is as follows: <? Php
/**
* Function: Paging class, based on the total amount of data provided and page size
* Creation date: Fri Apr 20 16:45:21 CST 2007
* Last Update:
* By sanshi <sanshi0815@tom.com>
*/

Class pagination
{
Var $ result = array ();
Var $ pVar = "myp"; // page parameter page count
Var $ urlParamStr = ""; // All parameters on the page
Var $ sqlMoveNumber = 0; // data offset
Var $ is_post = false;

Public function pagination ()
{

}
Public function set ($ sum, $ pageSize, $ page = "", $ is_post = false)
{
$ This-> is_post = $ is_post;
$ This-> pVar = defined ('page _ bar ')? PAGE_BAR: 'myp ';
$ Url = $ _ SERVER ['php _ SELF '].'? '. $ _ SERVER ['query _ string'];
// Calculate the total number of pages
$ CurPage = $ this-> getCurPage ();

$ Temp ['sumpage'] = ceil ($ sum/$ pageSize );
// Modified the bug where the page number exceeds
If ($ curPage> $ temp ['sumpage']) $ curPage = $ temp ['sumpage'];
$ Temp ['curpage'] = $ curPage;
$ Temp ['nextpage'] = $ temp ['sumpage']> $ curPage? ($ CurPage + 1): $ temp ['sumpage'];
$ Temp ['uppage'] = $ curPage> 1? $ CurPage-1: 1;
$ Temp ['endpage'] = $ temp ['sumpage'];
$ This-> urlParamStr = $ this-> _ makeUrl ($ url );
$ Temp ['urlpagestr'] = ($ page = "")? $ This-> _ makeUrl ($ url): $ page. $ this-> urlParamStr;

$ This-> sqlMoveNumber = $ curPage <2? 0: ($ curPage-1) * $ pageSize;
$ Temp ['sqlmovenumber'] = $ this-> sqlMoveNumber;
$ Temp ['rowno'] = $ sum;
$ This-> result = $ temp;
Return $ this-> result;
// Print_r ($ temp); exit ();
}
Public function getSqlMove ()
{
Return $ this-> sqlMoveNumber;
}
Public function getFooter ()
{
$ Str = "Total <font color = # FF0033 >{$ this-> result ['rowno']} </FONT> <a href = '{$ this-> result ['urlpagestr'] }{$ this-> result ['upgrade']} '";
If ($ this-> is_post) $ str. = "onclick = 'Return sanshi_page_post (this. href, {$ this-> result ['uppage']}); '";
$ Str. = "> previous page </a> ";
$ Str. = "Current {$ this-> result ['curpage']} page ";
$ Str. = "Total {$ this-> result ['sumpage']} pages ";
$ Str. = "<a href = '{$ this-> result ['urlpagestr']} {$ this-> result ['nextpage']}'";
If ($ this-> is_post) $ str. = "onclick = 'Return sanshi_page_post (this. href, {$ this-> result ['nextpage']}); '";
$ Str. = "> next page </a> ";
Return $ str;
}
Public function getJumpPage ()
{
If ($ this-> is_post)
{
$ Str = "Jump to the <select name = 'topage' size = '1' onchange = 'sanshi _ page_post (\" {$ this-> result ['urlpagestr']} \ "+ this. value, this. value) '> \ n ";
} Else {
$ Str = "Jump to the <select name = 'topage' size = '1' onchange = 'window. location = \ "{$ this-> result ['urlpagestr']} \" + this. value'> \ n ";
}
For ($ I = 1; $ I <= $ this-> result ['sumpage']; $ I ++)
{
$ Str. = ($ I ==$ this-> result ['curpage'])? "<Option value = '$ I' selected> $ I </option> \ n ": "<option value = '$ I'> $ I </option> \ n ";
}
$ Str. = "</select> page ";
// Output form
If ($ this-> is_post)
{
$ Str. = "<script type =" text/javascript "> <! --
Function sanshi_page_post (jump_link, page_no)
{
Document. getElementById ('sanshi _ gopage'). action = jump_link; document. getElementById ('{$ this-> pVar}'). value = page_no;
Document. getElementById ('sanshi _ gopage'). submit ();
Return false;
}

// --> </Script> ";
$ Str. = "<form name = 'sanshi _ gopage' id = 'sanshi _ gopage' method = post> ";
$ Str. = "<input type = \" hidden \ "name = '{$ this-> pVar}' id = '{$ this-> pVar}' value = 0> ";
$ _ POST = isset ($ _ POST )? $ _ POST: array ();
Foreach ($ _ post as $ k => $ v)
{
$ Str. = "<input type = \" hidden \ "name = \" {$ k} \ "id = \" {$ k} \ "value = \" {$ v }\ "> ";
}
$ Str. = "</form> ";
}
Return $ str;
}
Public function getCurPage ()
{
Return isset ($ _ GET [$ this-> pVar])? Intval ($ _ GET [$ this-> pVar]): 1;
}
// Analyze the url parameter returned? After the parameter, the page number is blank, including question mark
Private function _ makeUrl ($ url)
{
$ ArrayUrl = parse_url ($ url );
If (isset ($ arrayUrl ['query'])
{
$ Q = $ arrayUrl ['query'];
Parse_str ($ q, $ qParam );
// Print_r ($ qParam );
If (array_key_exists ($ this-> pVar, $ qParam ))
{
Foreach ($ qParam AS $ k => $ v)
{
If ($ k! = $ This-> pVar) $ temp [$ k] = $ v;
}
$ Temp [$ this-> pVar] = "";
If (function_exists ("http_build_query "))
Return '? '. Http_build_query ($ temp );
Else
Return '? '. Sanshi_http_build_query ($ temp );
} Else {
Return "? {$ Q} & {$ this-> pVar} = ";
}
} Else {
Return "? {$ This-> pVar} = ";
}

}
}
Function sanshi_http_build_query ($ a, $ B = '', $ c = 0)
{
If (! Is_array ($ a) return false;
Foreach (array) $ a as $ k => $ v)
{
If ($ c)
$ K = $ B. "[". $ k. "]";
Elseif (is_int ($ k ))
$ K = $ B. $ k;
If (is_array ($ v) | is_object ($ v ))
{
$ R [] = http_build_query ($ v, $ k, 1 );
Continue;
}
$ R [] = $ k. "=". urlencode ($ v );
}
Return implode ("&", $ r );
}
/*
// Print_r ($ _ SERVER );
$ P = new pagination ();
$ P-> set (10, 2, 'thi. php ');
Echo $ p-> getFooter ();
Echo $ p-> getJumpPage ();
*/
?>

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.