PHP simple and beautiful paging class

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. This article introduces a native PHP paging class. The paging style is similar to bootstrap.

/*************************************** ********
* @ Class Name: page
* @ Parameter: $ myde_total-Total number of records
* $ Myde_size-number of records displayed on one page
* $ Myde_page-current page
* $ Myde_url-get the current url
* @ Function: Paging implementation
* @ Author: Song haige
*/

Class page {

Private $ myde_total; // total number of records
Private $ myde_size; // number of records displayed on one page
Private $ myde_page; // current page
Private $ myde_page_count; // total number of pages
Private $ myde_ I; // start page
Private $ myde_en; // The ending page number.
Private $ myde_url; // obtain the current url
/*
* $ Show_pages
* Page display format. The page number of The Link displayed is 2 * $ show_pages + 1.
* For example, $ show_pages = 2, the page is displayed on the [homepage] [Previous Page] 1 2 3 4 5 [Next Page] [Last Page]
*/
Private $ show_pages;

Public function _ construct ($ myde_total = 1, $ myde_size = 1, $ myde_page = 1, $ myde_url, $ show_pages = 2 ){
$ This-> myde_total = $ this-> numeric ($ myde_total );
$ This-> myde_size = $ this-> numeric ($ myde_size );
$ This-> myde_page = $ this-> numeric ($ myde_page );
$ This-> myde_page_count = ceil ($ this-> myde_total/$ this-> myde_size );
$ This-> myde_url = $ myde_url;
If ($ this-> myde_total <0)
$ This-> myde_total = 0;
If ($ this-> myde_page <1)
$ This-> myde_page = 1;
If ($ this-> myde_page_count <1)
$ This-> myde_page_count = 1;
If ($ this-> myde_page> $ this-> myde_page_count)
$ This-> myde_page = $ this-> myde_page_count;
$ This-> limit = ($ this-> myde_page-1) * $ this-> myde_size;
$ This-> myde_ I = $ this-> myde_page-$ show_pages;
$ This-> myde_en = $ this-> myde_page + $ show_pages;
If ($ this-> myde_ I <1 ){
$ This-> myde_en = $ this-> myde_en + (1-$ this-> myde_ I );
$ This-> myde_ I = 1;
}
If ($ this-> myde_en> $ this-> myde_page_count ){
$ This-> myde_ I = $ this-> myde_ I-($ this-> myde_en-$ this-> myde_page_count );
$ This-> myde_en = $ this-> myde_page_count;
}
If ($ this-> myde_ I <1)
$ This-> myde_ I = 1;
}

// Check whether it is a number
Private function numeric ($ num ){
If (strlen ($ num )){
If (! Preg_match ("/^ [0-9] + $/", $ num )){
$ Num = 1;
} Else {
$ Num = substr ($ num, 0, 11 );
}
} Else {
$ Num = 1;
}
Return $ num;
}

// Address replacement
Private function page_replace ($ page ){
Return str_replace ("{page}", $ page, $ this-> myde_url );
}

// Homepage
Private function myde_home (){
If ($ this-> myde_page! = 1 ){
Return "page_replace (1)." 'title = 'homepage'> Homepage ";
} Else {
Return"

Homepage

";
}
}

// Previous Page
Private function myde_prev (){
If ($ this-> myde_page! = 1 ){
Return "page_replace ($ this-> myde_page-1)." 'title = 'previous page'> previous page_replace ";
} Else {
Return"

Previous Page

";
}
}

// Next page
Private function myde_next (){
If ($ this-> myde_page! = $ This-> myde_page_count ){
Return "page_replace ($ this-> myde_page + 1)." 'title = 'Next page'> next page_replace ";
} Else {
Return"

Next Page

";
}
}

// Last page
Private function myde_last (){
If ($ this-> myde_page! = $ This-> myde_page_count ){
Return "page_replace ($ this-> myde_page_count)." 'title = 'end page'> end page ";
} Else {
Return"

Last page

";
}
}

// Output
Public function myde_write ($ id = 'page '){
$ Str ="

";
$ Str. = $ this-> myde_home ();
$ Str. = $ this-> myde_prev ();
If ($ this-> myde_ I> 1 ){
$ Str. ="

...

";
}
For ($ I = $ this-> myde_ I; $ I <= $ this-> myde_en; $ I ++ ){
If ($ I ==$ this-> myde_page ){
$ Str. = "page_replace ($ I)." 'title = 'No. ". $ I." Page 'class = 'cur'> $ I ";
} Else {
$ Str. = "page_replace ($ I)." 'title = 'No. ". $ I." Page'> $ I ";
}
}
If ($ this-> myde_en <$ this-> myde_page_count ){
$ Str. ="

...

";
}
$ Str. = $ this-> myde_next ();
$ Str. = $ this-> myde_last ();
$ Str. ="

Total". $ This-> myde_page_count.
"
Page". $ This-> myde_total ."Data entries

";
$ Str. ="

";
Return $ str;
}

}

?>
Paging demonstrationAddress: http://www.sucaihuo.com/php/223.html

PHP simple and beautiful page category. Zip (3.79 KB download: 453 times)

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.