A simple and pure PHP paging class

Source: Internet
Author: User
A simple and pure PHP paging class

His features: simple, accept parameters, and give pages; flexibility: you can decide the name of the top or Next page (for example, prev, nex t, and so on) you can determine the number of pages displayed on each page. a packaging method is provided to adapt to different webpage structures. for example, if a webpage contains (

  • 1
  • ...), And so on. note: There are two static methods in the demo code that you can ignore. This is used for project development. Call: import. you can use include to replace URL: refresh. this is an address Operation class. we will not describe it for the time being. we will have a chance to release it later, you can use this page method getUrl () http://www.oschin a.net/code/snippet_182375_6242

    [PHP] code

    1. /**
    2. * FILE_NAME: Pages. php FILE_PATH:/lv/view/
    3. * Paging class
    4. *
    5. * @ Copyright Copyright (c) 2006-2010 mailTo: levi@cgfeel.com
    6. * @ Author Levi
    7. * @ Package lv. view. Pages
    8. * @ Subpackage
    9. * @ Version 2011-10-03
    10. */
    11. Call: import ('LV. url. URL ');
    12. Class Pages
    13. {
    14. Public $ num = 1;
    15. Public $ size = 50;
    16. Public $ current = 1;
    17. Private $ _ pages = array ();
    18. Private $ _ title = array ();
    19. Public function _ construct ($ count, $ size = 50)
    20. {
    21. $ This-> num = ceil ($ count/$ size );
    22. $ Size> 0 & $ this-> size = (Int) $ size;
    23. $ Page = isset ($ _ GET ['Page'])? (Int) trim ($ _ GET ['Page']): 1;
    24. $ Page> 1 & $ this-> current = (Int) $ page;
    25. $ This-> _ title = array ('previous page', '1 .. ', $ this-> num.' .. ', 'next page ');
    26. }
    27. /**
    28. * Packaging page
    29. * @ Param String | Array $ skirt
    30. * @ Param Array $ entitle
    31. * @ Return String
    32. */
    33. Public function warp ($ skirt, $ entitle = array ())
    34. {
    35. Empty ($ this-> _ pages) & $ this-> get ();
    36. $ Entitle + = $ this-> _ title;
    37. $ Skirt = (Array) $ skirt + array ('', NULL );
    38. $ Data = implode ($ skirt [1]. $ skirt [0], $ this-> _ pages );
    39. ! Is_null ($ skirt [1]) & $ data = $ skirt [0]. $ data. $ skirt [1];
    40. Return vsprintf ($ data, $ entitle );
    41. }
    42. /**
    43. * Retrieve pages
    44. * @ Param Int $ num: displays the number of pages.
    45. * @ Param Int $ span paging interval
    46. */
    47. Public function get ($ num = '5', $ span = '2 ')
    48. {
    49. $ This-> _ pages = array ();
    50. $ Start = $ this-> current-$ num + $ span;
    51. $ Start <1 & $ start = 1;
    52. $ End = $ start + $ num;
    53. $ End> $ this-> num & $ end = (Int) $ this-> num;
    54. $ This-> current> 1 & $ this-> _ pages [] = $ this-> _ setPage ($ this-> current-1, '% 1 $ s ');
    55. $ Start> 1 & $ this-> _ pages [] = $ this-> _ setPage (NULL, '% 2 $ s ');
    56. For ($ I = $ start; $ I <= $ end; $ I ++) $ this-> _ pages [] = $ this-> _ setPage ($ I, $ I );
    57. $ End <$ this-> num & $ this-> _ pages [] = $ this-> _ setPage ($ this-> num, '% 3 $ s ');
    58. $ This-> current <$ this-> num & $ this-> _ pages [] = $ this-> _ setPage ($ this-> current + 1, '% 4 $ s ');
    59. Return (Array) $ this-> _ pages;
    60. }
    61. Private function _ setPage ($ page, $ name = '% s ')
    62. {
    63. $ Hover = $ page ==$ this-> current? 'Class = "pageHover "':'';
    64. Return sprintf ('% s', URL: refresh (array ('page' => $ page), $ hover, $ name );
    65. }
    66. }
    67. ?>

    Demonstration

    1. $ Pages = new Pages (100, 20 );
    2. $ Pages-> warp ('| ');

    Paging, PHP

    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.