Powerful php mysql paging code

Source: Internet
Author: User

<? PHP
Class page
{
VaR $ page_name = "page ";
VaR $ next_page = '>'; // next page
VaR $ pre_page = '<'; // Previous Page
VaR $ first_page = 'VaR $ last_page = 'last page> '; // last page
VaR $ pre_bar = '<'; // The last page
VaR $ next_bar = '>'; // next page
VaR $ format_left = '';
VaR $ format_right = '';
VaR $ page_webmode = '';

/**
* Private
*
*/
VaR $ pagebarnum = 10; // control the number of records.
VaR $ totalpage = 0; // total number of pages
VaR $ nowindex = 1; // current page
VaR $ url = ""; // URL Header
VaR $ offset = 0;
 
/**
* Constructor Constructor
*
* @ Param array $ array ['Total'], $ array ['perpage'], $ array ['nowindex '], $ array ['url'], $ array ['ajax ']...
*/
Function page ($ array)
{
If (is_array ($ array )){
If (! Array_key_exists ('Total', $ array) $ this-> error (_ FUNCTION __, 'Need a param of total ');
$ Total = intval ($ array ['Total']);
$ Perpage = (array_key_exists ('perpage', $ array ))? Intval ($ array ['perpage']): 10;
$ Nowindex = (array_key_exists ('nowindex ', $ array ))? Intval ($ array ['nowindex ']): '';
$ Url = (array_key_exists ('url', $ array ))? $ Array ['url']: '';
} Else {
$ Total = $ array;
$ Perpage = 10;
$ Nowindex = '';
$ Url = '';
}
If ((! Is_int ($ total) | ($ total <0) $ this-> error (_ FUNCTION __, $ total. 'is not a positive integer! ');
If ((! Is_int ($ perpage) | ($ perpage <= 0) $ this-> error (_ FUNCTION __, $ perpage. 'is not a positive integer! ');
If (! Empty ($ array ['page _ name']) $ this-> set ('page _ name', $ array ['page _ name']); // set pagename
$ This-> _ set_nowindex ($ nowindex); // you can specify the current page.
$ This-> _ set_url ($ url); // sets the link address.
$ This-> totalpage = ceil ($ total/$ perpage );
$ This-> total = $ total;
$ This-> offset = ($ this-> nowindex-1) * $ perpage;
}
/**
* Set the value of the variable name specified in the class. If the change volume does not belong to this class, throw an exception
*
* @ Param string $ var
* @ Param string $ value
*/
Function set ($ var, $ value)
{
If (in_array ($ var, get_object_vars ($ this )))
$ This-> $ var = $ value;
Else {
$ This-> error (_ FUNCTION __, $ var. "does not belong to PB_Page! ");
}

}
/**
* Get the code that displays the next page.
*
* @ Param string $ style
* @ Return string
*/
Function next_page ($ style = '', $ nowindex_style = '')
{
If ($ this-> nowindex <$ this-> totalpage ){
Return $ this-> _ get_link ($ this-> _ get_url ($ this-> nowindex + 1), $ this-> next_page, $ style );
}
Return '<span class = "'. $ nowindex_style. '">'. $ this-> next_page. '</span> ';
}
 
/**
* Get the code for displaying the "Previous Page"
*
* @ Param string $ style
* @ Return string
*/
Function pre_page ($ style = '', $ nowindex_style = '')
{
If ($ this-> nowindex> 1 ){
Return $ this-> _ get_link ($ this-> _ get_url ($ this-> nowindex-1), $ this-> pre_page, $ style );
}
Return '<span class = "'. $ nowindex_style. '">'. $ this-> pre_page. '</span> ';
}
 
/**
* Get the code for displaying the "Homepage"
*
* @ Return string
*/
Function first_page ($ style = '', $ nowindex_style = '')
{
If ($ this-> nowindex = 1 ){
Return '<span class = "'. $ nowindex_style. '">'. $ this-> first_page. '</span> ';
}
Return $ this-> _ get_link ($ this-> _ get_url (1), $ this-> first_page, $ style );
}
 
/**
* Get the code for displaying the "Last page"
*
* @ Return string
*/
Function last_page ($ style = '', $ nowindex_style = '')
{
If ($ this-> nowindex ==$ this-> totalpage ){
Return '<span class = "'. $ nowindex_style. '">'. $ this-> last_page. '</span> ';
}
Return $ this-> _ get_link ($ this-> _ get_url ($ this-> totalpage), $ this-> last_page, $ style );
}
 
Function nowbar ($ style = '', $ nowindex_style = '')
{
$ Plus = Ceil ($ this-> pagebarnum/2 );
If ($ this-> pagebarnum-$ plus + $ this-> nowindex> $ this-> totalpage) $ plus = ($ this-> pagebarnum-$ this-> totalpage + $ this-> nowindex );
$ Begin = $ this-> nowindex-$ plus + 1;
$ Begin = ($ begin> = 1 )? $ Begin: 1;
$ Return = '';
For ($ I = $ begin; $ I <$ begin + $ this-> pagebarnum; $ I ++)
{
If ($ I <= $ this-> totalpage ){
If ($ I! = $ This-> nowindex)
$ Return. = $ this-> _ get_text ($ this-> _ get_link ($ this-> _ get_url ($ I), $ I, $ style ));
Else
$ Return. = $ this-> _ get_text ('<span class = "'. $ nowindex_style. '">'. $ I. '</span> ');
} Else {
Break;
}
$ Return. = "/n ";
}
Unset ($ begin );
Return $ return;
}
/**
* Get the code for displaying the jump button
*
* @ Return string
*/
Function select ()
{
$ Return = '<select name = "PB_Page_Select" onchange = "self. location. href = /''. $ this-> url. '/' + this. options [this. selectedIndex]. value "> ';
For ($ I = 1; $ I <= $ this-> totalpage; $ I ++)
{
If ($ I ==$ this-> nowindex ){
$ Return. = '<option value = "'. $ I. '" selected>'. $ I. '</option> ';
} Else {
$ Return. = '<option value = "'. $ I. '">'. $ I. '</option> ';
}
}
Unset ($ I );
$ Return. = '</select> ';
Return $ return;
}
 
/**
* Obtain the limit value in the mysql statement.
*
* @ Return string
*/
Function offset ()
{
Return $ this-> offset;
}
 
/**
* Control the display style by PAGE (you can add the corresponding style)
*
* @ Param int $ Mode
* @ Return string
*/
Function show ($ mode = 1, $ style = '', $ nowindex_style = '')
{
Switch ($ Mode)
{
Case '1 ':
$ This-> next_page = 'Next page ';
$ This-> pre_page = 'previous page ';
$ This-> first_page = 'homepage ';
$ This-> last_page = 'last page ';
Return $ this-> first_page ($ style, $ nowindex_style ). "". $ this-> pre_page ($ style, $ nowindex_style ). "". $ this-> next_page ($ style, $ nowindex_style ). "". $ this-> last_page ($ style, $ nowindex_style ). '(current <span style = "color: # C16012; font-size: 12pt;"> '. $ this-> nowindex. '</span> page <span style = "color: # C16012; font-size: 12pt;"> '. $ this-> totalpage. '</span> page <span style = "color: # C16012; font-size: 12pt;"> '. $ this-> total. '</span> records )';
Break;
Case '2 ':
$ This-> next_page = 'Next page ';
$ This-> pre_page = 'previous page ';
$ This-> first_page = 'homepage ';
$ This-> last_page = 'last page ';
Return $ this-> first_page ($ style, $ nowindex_style ). "". $ this-> pre_page ($ style, $ nowindex_style ). "". $ this-> next_page ($ style, $ nowindex_style ). "". $ this-> last_page ($ style, $ nowindex_style ). 'di '. $ this-> select (). 'page (current section <span style = "color: # c16012; font-size: 12pt;"> '. $ this-> nowindex. '</span> page <span style = "color: # c16012; font-size: 12pt;"> '. $ this-> totalpage. '</span> page <span style = "color: # c16012; font-size: 12pt;"> '. $ this-> total. '</span> records )';
Break;
Case '3 ':
$ This-> next_page = 'Next page ';
$ This-> pre_page = 'previous page ';
$ This-> first_page = 'homepage ';
$ This-> last_page = 'last page ';
Return $ this-> pre_page ($ style, $ nowindex_style ). "". $ this-> nowbar ($ style, $ nowindex_style ). "". $ this-> next_page ($ style, $ nowindex_style ). '(current <span style = "color: # C16012; font-size: 12pt;"> '. $ this-> nowindex. '</span> page <span style = "color: # C16012; font-size: 12pt;"> '. $ this-> totalpage. '</span> page <span style = "color: # C16012; font-size: 12pt;"> '. $ this-> total. '</span> records )';
Break;
Case '4 ':
$ This-> next_page = 'Next page ';
$ This-> pre_page = 'previous page ';
Return $ this-> pre_page ($ style, $ nowindex_style ). "". $ this-> nowbar ($ style, $ nowindex_style ). "". $ this-> next_page ($ style, $ nowindex_style ). 'di '. $ this-> select (). 'page (current section <span style = "color: # C16012; font-size: 12pt;"> '. $ this-> nowindex. '</span> page <span style = "color: # C16012; font-size: 12pt;"> '. $ this-> totalpage. '</span> page <span style = "color: # C16012; font-size: 12pt;"> '. $ this-> total. '</span> records )';
Break;
Case '5 ':
$ This-> next_page = '> ';
$ This-> pre_page = '<';
$ This-> first_page = '<';
$ This-> last_page = '> ';
Return $ this-> first_page ($ style, $ nowindex_style ). "". $ this-> pre_page ($ style, $ nowindex_style ). "". $ this-> nowbar ($ style, $ nowindex_style ). "". $ this-> next_page ($ style, $ nowindex_style ). "". $ this-> last_page ($ style, $ nowindex_style ). '(current <span style = "color: # C16012; font-size: 12pt;"> '. $ this-> nowindex. '</span> page <span style = "color: # C16012; font-size: 12pt;"> '. $ this-> totalpage. '</span> page <span style = "color: # C16012; font-size: 12pt;"> '. $ this-> total. '</span> records )';
Break;


Case '6 ':
$ Pagehtml = "<div> ";
If ($ this-> nowindex> 1)
{
$ Pagehtml. = "<SPAN class =/" pagenum-L/"> <a href =/" {$ this-> _ get_url ($ this-> nowindex-1 )} /"> </a> </span> ";
}
Else
{
$ Pagehtml. = "<SPAN class =/" pagenum-L/"> </span> ";
}
$ Pagehtml. = "<SPAN class =/" pagenum-C/"> ";
$ Pagehtml. = "<Table> <tr> ";

$ Plus = Ceil ($ this-> pagebarnum/2 );
If ($ this-> pagebarnum-$ plus + $ this-> nowindex> $ this-> totalpage) $ plus = ($ this-> pagebarnum-$ this-> totalpage + $ this-> nowindex );
$ Begin = $ this-> nowindex-$ plus + 1;
$ Begin = ($ begin> = 1 )? $ Begin: 1;
For ($ I = $ begin; $ I <$ begin + $ this-> pagebarnum; $ I ++)
{
If ($ I <= $ this-> totalpage ){
If ($ I! = $ This-> nowindex)
$ Pagehtml. = "<TD> & nbsp; <a href =/" {$ this-> _ get_url ($ I)}/">{$ I} </a> & nbsp; </TD> ";
Else
$ Pagehtml. = "<TD> & nbsp; {$ I} & nbsp; </TD> ";
} Else {
Break;
}
$ Pagehtml. = "/N ";
}
Unset ($ begin );



$ Pagehtml. = "</tr> </table> ";
$ Pagehtml. = "</span> ";


If ($ this-> nowindex <$ this-> totalpage)
{
$ Pagehtml. = "<span class =/" pagenum-r/"> <a href =/" {$ this-> _ get_url ($ this-> nowindex + 1 )} /"> </a> </span> ";
}
Else
{
$ Pagehtml. = "<span class =/" pagenum-r/"> </span> ";
}
$ Pagehtml. = "</div> ";
Return $ pagehtml;

Break;


}
}
/* ---------------- Private function (private method )-----------------------------------------------------------*/
/**
* Set the url header address
* @ Param: String $ url
* @ Return boolean
*/
Function _ set_url ($ url = "")
{
If ($ this-> page_webmode! = "Php" & $ url! = ""){
$ This-> url = $ url;
}
Else {
If (! Empty ($ url )){
// Manually set
$ This-> url = $ url. (stristr ($ url ,'? '))? '&':'? '). $ This-> page_name. "= ";
} Else {
// Automatically obtain
If (empty ($ _ SERVER ['query _ string']) {
// When QUERY_STRING does not exist
$ This-> url = $ _ SERVER ['request _ URI ']. "? ". $ This-> page_name." = ";
} Else {
//
If (stristr ($ _ SERVER ['query _ string'], $ this-> page_name. '= ')){
// The Address has page parameters.
$ This-> url = str_replace ($ this-> page_name. '='. $ this-> nowindex, '', $ _ SERVER ['request _ URI ']);
$ Last = $ this-> url [strlen ($ this-> url)-1];
If ($ last = '? '| $ Last = '&'){
$ This-> url. = $ this-> page_name. "= ";
} Else {
$ This-> url. = '&'. $ this-> page_name. "= ";
}
} Else {
//
$ This-> url = $ _ SERVER ['request _ URI '].' & '. $ this-> page_name.' = ';
} // End if
} // End if
} // End if
}
}
 
/**
* Set the current page
*
*/
Function _ set_nowindex ($ nowindex)
{
If (empty ($ nowindex )){
// System Retrieval

If (isset ($ _ GET [$ this-> page_name]) {
$ This-> nowindex = intval ($ _ GET [$ this-> page_name]);
}
} Else {
// Manually set
$ This-> nowindex = intval ($ nowindex );
}
}

/**
* Return the address value for the specified page.
*
* @ Param int $ pageno
* @ Return string $ url
*/
Function _ get_url ($ pageno = 1)
{
If ($ this-> page_webmode = "php" | $ this-> page_webmode = ""){
Return $ this-> url. $ pageno;
}
Else {
Return $ this-> url. ($ pageno = 1 )? "": "_". $ Pageno). ".". $ this-> page_webmode;
}
}
 
/**
* Get the text displayed by page. For example, by default, _ get_text ('<a href = ""> 1 </a> ') [<a href = ""> 1 </a>]
*
* @ Param String $ str
* @ Return string $ url
*/
Function _ get_text ($ str)
{
Return $ this-> format_left. $ str. $ this-> format_right;
}
 
/**
* Obtain the link address
*/
Function _ get_link ($ url, $ text, $ style = ''){
$ Style = (empty ($ style ))? '': 'Class =" '. $ style .'"';
Return '<a'. $ style. 'href = "'. $ url.'"> '. $ text.' </a> ';
}
/**
* Error Handling Method
*/
Function error ($ function, $ errormsg)
{
Die ('error in file <B> '. _ FILE __. '</B>, Function <B> '. $ function. '() </B> :'. $ errormsg );
}
}
?>

 

 

Call:

 

 

<?
Include_once ("lib/my_page_class.php ");
$ Page = new page (array ('Total' => $ total, 'perpage' => $ pagesize ));
$ Pagehtml = $ page-> show (6, "page", "curr ");
Echo $ pagehtml;
?>

 

 

 

 

 

 

 

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.