[Recommended] a paging class for solving custom Routing

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn [recommended] a paging class for solving custom Routing
//----------------------------------------------------------
// Declaration: This code is not perfect. It may not be good. Please speak out!
// Author: xiaozeng
// Deduction: 839024615
// Website: www.yun8888.net
// About my ThinkPHP http://www.thinkphp1.cn/u/87696.html
//----------------------------------------------------------





// Many people are asking, if we re-define the route for the list
// The original address is list/index/id/1.html.
// List/1.html after Definition
// When you are in the system paging class, it will jump to list/index/id/1/p/2.html
// We can get a new class.


// Usage

Vendor ('page. Page # class ');
$ Params = array (
'Total _ rows '=> $ count, # (required)
'Method' => 'html', # (required)
'Parameter '=>'/list/'. $ id.'/p /?. Html ', # (required)
'Now _ page' => $ p, # (required)
'List _ rows '=> 30, # (optional) The default value is 15.
);

$ Page = new Core_Lib_Page ($ params );

$ This-> assign ("page", $ page-> show (1 ));








/**
* Paging class
* @ Author xiaojiong & 290747680@qq.com
* @ Date 2011-08-17
*
* Show (2) 1... 62 63 64 65 66 67 68... 150
* Paging Style
* # Page {font: 12px/16px arial}
* # Page span {float: left; margin: 0px 3px ;}
* # Page a {float: left; margin: 0 3px; border: 1px solid # ddd; padding: 3px 7px; text-decoration: none; color: #666}
* # Page a. now_page, # page a: hover {color: # fff; background: # 05c}
*/

Class Core_Lib_Page
{
Public $ first_row; // The start row.

Public $ list_rows; // The number of lines displayed on each page in the list

Protected $ total_pages; // total number of pages

Protected $ total_rows; // total number of rows

Protected $ now_page; // current page number

Protected $ method = 'delete'; // processing Ajax paging Html paging (static) normal get Mode

Protected $ parameter = '';

Protected $ page_name; // name of the paging Parameter

Protected $ ajax_func_name;

Public $ plus = 3; // page offset

Protected $ url;


/**
* Constructor
* @ Param unknown_type $ data
*/
Public function _ construct ($ data = array ())
{
$ This-> total_rows = $ data ['total _ rows '];

$ This-> parameter =! Empty ($ data ['parameter '])? $ Data ['parameter ']: '';
$ This-> list_rows =! Empty ($ data ['list _ rows ']) & $ data ['list _ rows'] <= 100? $ Data ['list _ rows ']: 15;
$ This-> total_pages = ceil ($ this-> total_rows/$ this-> list_rows );
$ This-> page_name =! Empty ($ data ['page _ name'])? $ Data ['page _ name']: 'P ';
$ This-> ajax_func_name =! Empty ($ data ['ajax _ func_name '])? $ Data ['ajax _ func_name ']: '';

$ This-> method =! Empty ($ data ['method'])? $ Data ['method']: '';


/* Current page */
If (! Empty ($ data ['now _ page'])
{
$ This-> now_page = intval ($ data ['now _ page']);
} Else {
$ This-> now_page =! Empty ($ _ GET [$ this-> page_name])? Intval ($ _ GET [$ this-> page_name]): 1;
}
$ This-> now_page = $ this-> now_page <= 0? 1: $ this-> now_page;


If (! Empty ($ this-> total_pages) & $ this-> now_page> $ this-> total_pages)
{
$ This-> now_page = $ this-> total_pages;
}
$ This-> first_row = $ this-> list_rows * ($ this-> now_page-1 );
}

/**
* Get the current connection
* @ Param $ page
* @ Param $ text
* @ Return string
*/
Protected function _ get_link ($ page, $ text)
{
Switch ($ this-> method ){
Case 'ajax ':
$ Parameter = '';
If ($ this-> parameter)
{
$ Parameter = ','. $ this-> parameter;
}
Return 'ajax _ func_name. '(\''. $ page. '\''. $ parameter. ') "href =" javascript: void (0) "> '. $ text. ''. "\ n ";
Break;

Case 'html ':
$ Url = str_replace ('? ', $ Page, $ this-> parameter );
Return ''. $ text.''. "\ n ";
Break;

Default:
Return '_ get_url ($ page).' "> '. $ text.'." \ n ";
Break;
}
}


/**
* Set the current page Link
*/
Protected function _ set_url ()
{
$ Url = $ _ SERVER ['request _ URI ']. (strpos ($ _ SERVER ['request _ URI'], '? ')? '':"? "). $ This-> parameter;
$ Parse = parse_url ($ url );
If (isset ($ parse ['query']) {
Parse_str ($ parse ['query'], $ params );
Unset ($ params [$ this-> page_name]);
$ Url = $ parse ['path']. '? '. Http_build_query ($ params );
}
If (! Empty ($ params ))
{
$ Url. = '&';
}
$ This-> url = $ url;
}

/**
* Get the url of $ page.
* @ Param $ page
* @ Return string
*/
Protected function _ get_url ($ page)
{
If ($ this-> url = NULL)
{
$ This-> _ set_url ();
}
// $ Lable = strpos ('&', $ this-> url) === FALSE? '':'&';
Return $ this-> url. $ this-> page_name. '='. $ page;
}


/**
* The first page is displayed.
* @ Return string
*/
Public function first_page ($ name = 'page 1 ')
{
If ($ this-> now_page> 5)
{
Return $ this-> _ get_link ('1', $ name );
}
Return '';
}

/**
* Last Page
* @ Param $ name
* @ Return string
*/
Public function last_page ($ name = 'last page ')
{
If ($ this-> now_page <$ this-> total_pages-5)
{
Return $ this-> _ get_link ($ this-> total_pages, $ name );
}
Return '';
}

/**
* Previous Page
* @ Return string
*/
Public function up_page ($ name = 'previous page ')
{
If ($ this-> now_page! = 1)
{
Return $ this-> _ get_link ($ this-> now_page-1, $ name );
}
Return '';
}

/**
* Next Page
* @ Return string
*/
Public function down_page ($ name = 'Next page ')
{
If ($ this-> now_page <$ this-> total_pages)
{
Return $ this-> _ get_link ($ this-> now_page + 1, $ name );
}
Return '';
}

/**
* Paging style output
* @ Param $ param
* @ Return string
*/
Public function show ($ param = 1)
{
If ($ this-> total_rows <1)
{
Return '';
}

$ ClassName = 'show _ '. $ param;

$ ClassNames = get_class_methods ($ this );

If (in_array ($ className, $ classNames ))
{
Return $ this-> $ className ();
}
Return '';
}

Protected function show_2 ()
{
If ($ this-> total_pages! = 1)
{
$ Return = '';
$ Return. = $ this-> up_page ('<');
For ($ I = 1; $ I <= $ this-> total_pages; $ I ++)
{
If ($ I ==$ this-> now_page)
{
$ Return. = "$ I \ n ";
}
Else
{
If ($ this-> now_page-$ I >=4 & $ I! = 1)
{
$ Return. = "... \ n ";
$ I = $ this-> now_page-3;
}
Else
{
If ($ I >=$ this-> now_page + 5 & $ I! = $ This-> total_pages)
{
$ Return. = "... \ n ";
$ I = $ this-> total_pages;
}
$ Return. = $ this-> _ get_link ($ I, $ I). "\ n ";
}
}
}
$ Return. = $ this-> down_page ('> ');
Return $ return;
}
}

Protected function show_1 ()
{
$ Plus = $ this-> plus;
If ($ plus + $ this-> now_page> $ this-> total_pages)
{
$ Begin = $ this-> total_pages-$ plus * 2;
} Else {
$ Begin = $ this-> now_page-$ plus;
}

$ Begin = ($ begin> = 1 )? $ Begin: 1;
$ Return = '';
$ Return. = $ this-> first_page ();
$ Return. = $ this-> up_page ();
For ($ I = $ begin; $ I <= $ begin + $ plus * 2; $ I ++)
{
If ($ I> $ this-> total_pages)
{
Break;
}
If ($ I ==$ this-> now_page)
{
$ Return. = "$ I \ n ";
}
Else
{
$ Return. = $ this-> _ get_link ($ I, $ I). "\ n ";
}
}
$ Return. = $ this-> down_page ();
$ Return. = $ this-> last_page ();
Return $ return;
}

Protected function show_3 ()
{
$ Plus = $ this-> plus;
If ($ plus + $ this-> now_page> $ this-> total_pages)
{
$ Begin = $ this-> total_pages-$ plus * 2;
} Else {
$ Begin = $ this-> now_page-$ plus;
}
$ Begin = ($ begin> = 1 )? $ Begin: 1;
$ Return = 'Total '. $ this-> total_rows. 'records are classified '. $ this-> total_pages. 'page, current '. $ this-> now_page. 'page ';
$ Return. = ', each page ';
$ Return. ='';
$ Return. = $ this-> first_page (). "\ n ";
$ Return. = $ this-> up_page (). "\ n ";
$ Return. = $ this-> down_page (). "\ n ";
$ Return. = $ this-> last_page (). "\ n ";
$ Return. =''; For ($ I = $ begin; $ I <= $ begin + 10; $ I ++){If ($ I> $ this-> total_pages){Break;}If ($ I ==$ this-> now_page){$ Return. =''. $ I .'';}Else{$ Return. =''. $ I .'';}}$ Return. ='';
Return $ return;
}
}







//----------------------------------------------------------
// Declaration: This code is not perfect. It may not be good. Please speak out!
// Author: xiaozeng
// Deduction: 839024615
// Website: www.yun8888.net
// About my ThinkPHP http://www.thinkphp1.cn/u/87696.html
//----------------------------------------------------------

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.