Heim  >  Artikel  >  Backend-Entwicklung  >  一个简单、纯洁的PHP分页类

一个简单、纯洁的PHP分页类

WBOY
WBOYOriginal
2016-07-25 08:42:04867Durchsuche

他的特点:单纯,接受参数,给出分页;灵活:可以自己决定,上、下一页的名称(比如我想叫prev、nex t诸如此类)可以决定每页展示的分页数量,跨页数提供一个包装方法,适应不同的网页结构,比如有的网页我需要每个分页包含一个li(

  • 1
  • ...),依次类推备注说明:提供的示范代码中有两个静态方法大家可以不必理会。这个是自己项目开发用的。Call::import,大家可以使用include代 替URL::refresh,这是地址操作类,这里暂时不做说明,以后有机会再放上来,大家可以用这个页面的方法getUrl()http://www.oschin a.net/code/snippet_182375_6242

    [PHP]代码

    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('上一页', '1..', $this->num.'..', '下一页');
    26. }
    27. /**
    28. * 包装分页
    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. * 获取分页
    44. * @param Int $num 展示分页数
    45. * @param Int $span 分页间隔
    46. */
    47. public function get($num = '5', $span = '2')
    48. {
    49. $this->_pages = array();
    50. $start = $this->current - $num + $span;
    51. $start
    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 _pages[] = $this->_setPage($i, $i);
    57. $end num && $this->_pages[] = $this->_setPage($this->num, '%3$s');
    58. $this->current 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. ?>
    复制代码

    使用示范

    1. $pages = new Pages(100, 20);
    2. $pages->warp('|');
    复制代码
    分页, PHP


    Stellungnahme:
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
    Vorheriger Artikel:PHP的http请求处理类 Nächster Artikel:php获取汉字首字母