sugiphp / pagination
SugiPHP 分页组件
dev-master
2015-02-23 15:41 UTC
Requires
- php: >=5.3
This package is not auto-updated.
Last update: 2024-09-14 14:36:10 UTC
README
SugiPHP 分页组件是一个简单易用的类,为您的应用程序提供分页链接。您可以通过编写自定义渲染器或使用现有的渲染器(如Twitter的Bootstrap)来自定义页面链接的外观和感觉。
安装
如果您使用的是 composer,请在壳中输入,否则您需要下载Pagination和Exception类。
composer require sugiphp/pagination
基本用法
分页类本身不包含任何渲染,其主要目标是提供一个包含链接的数组。这可以轻松实现。
<?php use SugiPHP\Pagination\Pagination; $pagination = new Pagination(); $pagination->setTotalItems(45); // Set the total number of items $pages = $pagination->toArray(); // described below
如果网页的URL是 http://example.com/index.php?page=3,分页会猜测当前页面是3,并返回类似以下内容:
<?php var_dump($pages); // some parameters are removed for better readability [ 'prev' => [ 'page' => 2, 'uri' => '/index.php?page=1', 'isDisabled' => false ], 1 => [ 'page' => 1, 'uri' => '/index.php?page=1', 'isCurrent' => false, ], 2 => [ 'page' => 2, 'uri' => '/index.php?page=2', 'isCurrent' => false, ], 3 => [ 'page' => 3, 'uri' => '/index.php?page=3', 'isCurrent' => true, 'isDisabled' => true, ], 'next' => [ 'page' => 2, 'uri' => '#', 'isDisabled' => true ] ]
获取器
<?php /* * Returns current page number if it is set, or will try to guess it from the URL address. * Default is 1 */ $pagination->getPage(); /* * Returns the maximum number of items viewed in a single page. * Default is 20 */ $pagination->getItemsPerPage(); // getItemsPerPage() alias $pagination->getLimit(); /* * Returns URI pattern that is used to generate links and to guess current page. * Default is 'page={page}' */ $pagination->getPattern(); // Returns total number of pages based on total items and items per page settings. $pagination->getTotalPages(); /* * Returns first item's index in a page we are on. * Used primary in SQLs (e.g. SELECT * FROM test LIMIT 20 OFFSET 60) */ $pagination->getOffset(); /* * Returns proximity - how many page links should be in front and after current page. * Default is 4. * If there is not enough pages to display in front of the current page links * after will be more then proximity. So if you are on page 2 and proximity is 3 * the pages after the page 2 would not be 3, but 5. * Total number of links (items toArray() method returns) can be calculated by * proximity * 2 + 1 (current page) + 1 (previous) + 1 (next) + 1 (first) + 1 (last). * So if proximity is set to 4 total number of links will be 13 * if proximity is 3 total pages will be 11 * Note: Total number of links will be less if there are not enough pages to show. */ $pagination->getProximity(); // returns previously set total items $pagination->getTotalItems(); // getTotalItems() alias $pagination->getItems();
设置器
<?php // Total number of items. This one MUST be set. $pagination->setTotalItems($totalItems); // setTotalItems() alias $pagination->setItems($totalItems); // Sets the number of items (lines) in a single page. $pagination->setItemsPerPage($itemsPerPage); // setItemsPerPage() alias $pagination->setLimit($itemsParPage); // Sets the page number manually. $pagination->setPage($page); /* * Sets the URI pattern for creating links for pages. * Default pattern is "page={page}" (URLs like /posts/show?page=5) * Can be set for example to "p={page}" or anything else for $_GET parameter * Can be set also to "page/{page}" for friendly URLs. In this case Pagination * will build URLs like: /posts/show/page/5 */ $pagination->setPattern($pattern); /* * Sets the current URI. Default is $_SERVER["REQUEST_URI"] * Handy for unit tests. */ $pagination->setUri($uri); // Sets the proximity. See getProximity() above for more explanations. $pagination->setProximity($proximity);
每个设置都可以在分页构造函数中完成。
<?php $config = array( 'items' => 100, // or 'totalItems' 'itemsPerPage' => 10, // or 'ipp' 'proximity' => 3, 'uri' => 'http://example.com/show/page:6', 'pattern' => 'page:{page}', 'page' => 6, ); $pagination = new Pagination($config)
基本渲染器
<?php $pages = $pagination->toArray(); // Twitter Bootstrap 3 pagination $items = ""; foreach ($pages as $key => $page) { // link $href = $page["uri"]; // label if ($key === "prev") { $label = "«"; } elseif ($key === "next") { $label = "»"; } elseif ($key === "less" or $key === "more") { $label = "..."; } else { $label = $page["page"]; } // class if ($page["isCurrent"]) { $class = "active"; } elseif ($page["isDisabled"]) { $class = "disabled"; } else { $class = ""; } $items .= '<li class="'.$class.'"><a href="'.$href.'">'.$label.'</a></li>'; } echo '<ul class="pagination">' . $items . '</ul>';
您可以在项目的 示例 中查看更多渲染示例。