一個簡單、純潔的PHP分頁類

來源:互聯網
上載者:User

他的特點:單純,接受參數,給出分頁;靈活:可以自己決定,上、下一頁的名稱(比如我想叫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 < 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. ?>
    複製代碼

    使用示範

    1. $pages = new Pages(100, 20);
    2. $pages->warp('|');
    複製代碼
    分頁, PHP
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.