adamb/pagination

创建基于 Bootstrap 主题的分页按钮

1.1.6 2020-10-23 09:44 UTC

This package is auto-updated.

Last update: 2024-09-23 20:38:33 UTC


README

Build Status Scrutinizer Quality Score Scrutinizer Coverage Minimum PHP Version

分页

使用 PHP 创建 HTML 分页元素。与 Bootstrap 兼容,并且可以轻松自定义类。

安装

安装可通过 Composer/Packagist 完成,您可以将以下行添加到您的 composer.json 文件中

"adamb/pagination": "^1.0"

composer require adamb/pagination

许可证

本软件根据MIT 许可证分发。请阅读 LICENSE 以了解软件可用性和分发信息。

特性

  • 创建分页 HTML 元素
  • 兼容 Bootstrap 3/4
  • 更改分配给元素的类名
  • 轻松更改显示的最大链接数
  • 如果只有 1 页存在,则不会显示分页器

用法

1. 创建分页 HTML

PHP 代码

<?php

require "src\Pagination.php";

use Pager\Pagination;

$numberOfRecords = 342; // This can also be generated from a database query
$pageURL = '/results.php';
$currentPage = $_GET['page'];
$resultsPerPage = 50; // This is the default value so can be removed

$pagination = new Pagination();
echo($pagination->paging($numberOfRecords, $pageURL, $currentPage, $resultsPerPage));

HTML 输出

<ul class="pagination">
    <li class="active"><a href="/results.php?page=1" title="Page 1">1</a></li>
    <li><a href="/results.php?page=2" title="Page 2">2</a></li>
    <li><a href="/results.php?page=3" title="Page 3">3</a></li>
    <li><a href="/results.php?page=4" title="Page 4">4</a></li>
    <li><a href="/results.php?page=5" title="Page 5">5</a></li>
    <li><a href="/results.php?page=6" title="Page 6">6</a></li>
    <li><a href="/results.php?page=7" title="Page 7">7</a></li>
    <li><a href="/results.php?page=2" title="Page &gt;">&gt;</a></li>
    <li><a href="/results.php?page=7" title="Page &raquo;">&raquo;</a></li>
</ul>

2. 更改添加到 HTML 元素的类名

PHP 代码

<?php

require "src\Pagination.php";

use Pager\Pagination;

$numberOfRecords = 342; // This can also be generated from a database query
$pageURL = '/results.php';
$currentPage = 3; //$_GET['page'];

$pagination = new Pagination();

$pagination->setPaginationClass('my_custom_pager')
           ->setActiveClass('this_is_active');

echo($pagination->paging($numberOfRecords, $pageURL, $currentPage));

HTML 输出

<ul class="my_custom_pager">
    <li><a href="/results.php" title="Page &laquo;">&laquo;</a></li>
    <li><a href="/results.php?page=2" title="Page &lt;">&lt;</a></li>
    <li><a href="/results.php?page=1" title="Page 1">1</a></li>
    <li><a href="/results.php?page=2" title="Page 2">2</a></li>
    <li class="this_is_active"><a href="/results.php?page=3" title="Page 3">3</a></li>
    <li><a href="/results.php?page=4" title="Page 4">4</a></li>
    <li><a href="/results.php?page=5" title="Page 5">5</a></li>
    <li><a href="/results.php?page=6" title="Page 6">6</a></li>
    <li><a href="/results.php?page=7" title="Page 7">7</a></li>
    <li><a href="/results.php?page=4" title="Page &gt;">&gt;</a></li>
    <li><a href="/results.php?page=7" title="Page &raquo;">&raquo;</a></li>
</ul>

3. 其他特性

有一系列选项可供您针对项目进行自定义,这里您可以自定义一些事情,例如每页显示的项目数,这将增加或减少链接数。您还可以更改显示的最大按钮数,关闭箭头按钮的显示,并在 URL 查询字符串中添加其他项。

PHP 代码

<?php

require "src\Pagination.php";

use Pager\Pagination;

$numberOfRecords = 2042;
$pageURL = '/results.php';
$currentPage = 3; //$_GET['page'];
$resultsPerPage = 100;
$maximumLinksToDisplay = 9;
$displayArrows = false;
$additionalQuery = array(
    'search' => 'Hello', // urlencode($_GET['search']),
    'important_info' => 42, // urlencode($_GET['important_info']),
);

$pagination = new Pagination();

echo($pagination->paging($numberOfRecords, $pageURL, $currentPage, $resultsPerPage, $maximumLinksToDisplay, $displayArrows, $additionalQuery));

HTML 输出

<ul class="pagination">
    <li><a href="/results.php?search=Hello&amp;important_info=42&amp;page=1" title="Page 1">1</a></li>
    <li><a href="/results.php?search=Hello&amp;important_info=42&amp;page=2" title="Page 2">2</a></li>
    <li class="active"><a href="/results.php?search=Hello&amp;important_info=42&amp;page=3" title="Page 3">3</a></li>
    <li><a href="/results.php?search=Hello&amp;important_info=42&amp;page=4" title="Page 4">4</a></li>
    <li><a href="/results.php?search=Hello&amp;important_info=42&amp;page=5" title="Page 5">5</a></li>
    <li><a href="/results.php?search=Hello&amp;important_info=42&amp;page=6" title="Page 6">6</a></li>
    <li><a href="/results.php?search=Hello&amp;important_info=42&amp;page=7" title="Page 7">7</a></li>
    <li><a href="/results.php?search=Hello&amp;important_info=42&amp;page=8" title="Page 8">8</a></li>
    <li><a href="/results.php?search=Hello&amp;important_info=42&amp;page=9" title="Page 9">9</a></li>
</ul>