Home > Backend Development > PHP Tutorial > A simple and pure PHP paging class

A simple and pure PHP paging class

WBOY
Release: 2016-07-25 08:42:04
Original
947 people have browsed it

Its characteristics: simple, accepts parameters and gives pagination; flexible: you can decide by yourself. The names of the previous and next pages (for example, I want to call them prev, next, etc.) can determine the number of paginations displayed on each page and the number of cross-pages. Provide a packaging method to adapt to different web page structures. For example, for some web pages, I need each page to contain a li(

  • 1
  • ...), and so on. Note: There are two static methods in the provided sample code, which you can ignore. This is for my own project development. Call::import, you can use include instead of URL::refresh. This is an address operation class. I will not explain it here for now. I will put it up when I have the opportunity. You can use the method getUrl() on this page http://www. oschin a.net/code/snippet_182375_6242

    [PHP] code

    1. /**
    2. * FILE_NAME : Pages.php FILE_PATH : /lv/view/
    3. * 分页类
    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. * Package paging
    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. * Get pagination
    44. * @param Int $num display pagination number
    45. * @param Int $span pagination 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. ?>
    Copy code

    Usage Demonstration

    1. $pages = new Pages(100, 20);
    2. $pages->warp('|');
    Copy code
    Pagination, PHP


    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template