stefangabos/zebra_pagination

一个通用的、兼容Twitter Bootstrap的PHP分页库,可以自动生成导航链接

2.4.8 2024-06-19 15:28 UTC

This package is auto-updated.

Last update: 2024-09-19 16:57:23 UTC


README

zebrajs

Zebra Pagination  Tweet

一个通用的、兼容Twitter Bootstrap的,自动生成导航链接的分页库

Latest Stable Version Total Downloads Monthly Downloads Daily Downloads License

一个通用的、兼容Twitter Bootstrap(版本3、4和5)的分页脚本,根据总记录数和每页显示的记录数自动生成导航链接以及下一页/上一页链接。对于将大量数据分割成小块、减少网络流量以及同时提高可读性、美观性和易用性非常有用。

遵循分页最佳实践(提供大的可点击区域,不使用下划线,选中的页面清晰突出,页面链接间距适中,提供“上一页”和“下一页”链接,提供“第一页”和“最后一页”链接——如2007年Faruk Ates的一篇文章所述,现可在此找到此处,可以生成自然顺序和反向顺序的链接,可以轻松本地化,支持不同位置的下/上一页按钮,支持通过GET或通过URL重写进行页面传播,SEO友好,并且可以通过CSS轻松自定义外观)。

请注意,这是一个通用的分页脚本,意味着它不会显示任何记录,也没有任何对数据库连接或SQL查询的依赖,这使得它非常灵活!开发者需要根据分页脚本返回的信息获取实际数据并显示。优点是它可以用于对来自任何来源(如数组或数据库)的记录进行分页。

代码注释详尽,当PHP的错误报告级别设置为E_ALL时,不会产生警告/错误/通知。

特性

  • 这是一个通用库:可以用于从数组或数据库中分页记录
  • 它自动生成导航链接,给定总项目和每页项目数(还包括最佳实践示例)
  • 导航链接可以生成自然顺序或反向顺序
  • 它对SEO友好——解决了没有导航的第一页重复内容和URL中包含页码的第一页的问题
  • 外观可以通过CSS轻松自定义
  • 与Twitter Bootstrap版本3、4和5兼容
  • 代码注释详尽,当PHP的错误报告级别设置为E_ALL时,不会产生警告/错误/通知
  • 拥有出色的文档

📔 文档

查看出色的文档

🎂 支持此项目的发展

您的支持意义重大,它让我保持动力继续从事开源项目。
如果您喜欢这个项目,请通过点击页面顶部的星星按钮来 ⭐ 支持。
如果您愿意慷慨解囊,可以通过PayPal捐赠给我买咖啡,或者您可以成为赞助商。
无论如何 - 感谢您! 🎉

Star it on GitHub Donate

需求

PHP 5+

安装

您可以通过 Composer 安装 Zebra Pagination

# get the latest stable release
composer require stefangabos/zebra_pagination

# get the latest commit
composer require stefangabos/zebra_pagination:dev-master

或者您可以通过下载最新版本,解压,然后将其包含到您的项目中手动安装

require_once 'path/to/Zebra_Pagination.php';

如何使用

请确保在页面中包含以下内容:

<!-- you don't need this if you're using Twitter Bootstrap -->
<link rel="stylesheet" href="path/to/zebra_pagination.css" type="text/css">

如果您想保留URL中的哈希,也要包含JavaScript文件 - 简单地包含它就足够了;(jQuery需要在加载此文件之前加载)

<script src="path/to/zebra_pagination.js"></script>

从数组中分页数据

PHP

<?php

// let's paginate data from an array...
$countries = array(
    // array of countries
);

// how many records should be displayed on a page?
$records_per_page = 10;

// include the pagination class
// (you don't need this if you are using composer)
require 'path/to/Zebra_Pagination.php';

// instantiate the pagination object
$pagination = new Zebra_Pagination();

// the number of total records is the number of records in the array
$pagination->records(count($countries));

// records per page
$pagination->records_per_page($records_per_page);

// here's the magic: we need to display *only* the records for the current page
$countries = array_slice(
    $countries,
    (($pagination->get_page() - 1) * $records_per_page),
    $records_per_page
);

?>

<table>
    <thead>
    <tr>
        <th>Country</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($countries as $index => $country): ?>
    <tr<?php echo $index % 2 ? ' class="even"' : ''; ?>>
        <td><?php echo $country; ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>

<?php

// render the pagination links
$pagination->render();

结果将类似于

Zebra_Pagination, default layout

您可以使用 navigation_position() 方法将导航链接的位置设置为分页链接的左侧或右侧

$pagination->navigation_position('left');

Zebra_Pagination, position next/previous buttons on the left

$pagination->navigation_position('right');

Zebra_Pagination, position next/previous buttons on the right

可以使用 labels() 方法更改“上一页”和“下一页”链接的标签

$pagination->labels('Previous', 'Next');

Zebra_Pagination, using labels for navigating to next/previous pages

您也可以使用HTML标记作为标签,这使得包括类似Font Awesome的字体图标变得容易

$pagination->labels('<i class="fa fa-arrow-left"></i>', '<i class="fa fa-arrow-right"></i>');

Zebra_Pagination, using icons for navigating to next/previous pages

使用 紧凑导航,其中仅提供第一页、最后一页、下一页和上一页的链接。当没有足够空间进行完整分页时非常有用。

$pagination->condensed();

Zebra_Pagination, condensed navigation

使用 非常紧凑导航,其中仅提供下一页和上一页的链接。当没有足够空间进行完整分页时非常有用。

$pagination->condensed(true);

Zebra_Pagination, condensed navigation

从MySQL中分页数据

<?php

// connect to a database
$connection = mysqli_connect($host, $username, $password, $database);

// how many records should be displayed on a page?
$records_per_page = 10;

// include the pagination class
// (you don't need this if you are using composer)
require 'path/to/Zebra_Pagination.php';

// instantiate the pagination object
$pagination = new Zebra_Pagination();

// the MySQL statement to fetch the rows
$sql = '
    SELECT
        country
    FROM
        countries
    LIMIT
        ' . (($pagination->get_page() - 1) * $records_per_page) . ', ' . $records_per_page . '
';

// execute the MySQL query
// (you will use mysqli or PDO here, but you get the idea)
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));

// fetch the total number of records in the table
$rows = mysqli_fetch_assoc(mysqli_query($connection, 'SELECT COUNT(*) AS rows FROM countries'));

// pass the total number of records to the pagination class
$pagination->records($rows['rows']);

// records per page
$pagination->records_per_page($records_per_page);

?>

<table>
    <thead>
    <tr>
        <th>Country</th>
    </tr>
    </thead>
    <tbody>
    <?php $index = 0; while ($row = mysqli_fetch_assoc($result)): ?>
    <tr<?php echo $index++ % 2 ? ' class="even"' : ''; ?>>
        <td><?php echo $row['country']; ?></td>
    </tr>
    <?php endwhile; ?>
    </tbody>
</table>

<?php

// render the pagination links
$pagination->render();

以倒序从MySQL中分页数据

<?php

// connect to a database
$connection = mysqli_connect($host, $username, $password, $database);

// how many records should be displayed on a page?
$records_per_page = 10;

// include the pagination class
// (you don't need this if you are using Composer)
require 'path/to/Zebra_Pagination.php';

// instantiate the pagination object
$pagination = new Zebra_Pagination();

// show records in reverse order
$pagination->reverse(true);

// when showing records in reverse order, we need to know the total number
// of records from the beginning
$result = mysqli_query($connection, 'SELECT COUNT(*) AS rows FROM countries'))) or die (mysqli_error());

// pass the total number of records to the pagination class
$pagination->records(array_pop(mysqli_fetch_assoc($result)));

// records per page
$pagination->records_per_page($records_per_page);

// the MySQL statement to fetch the rows
// note the LIMIT - use it exactly like that!
// also note that we're ordering data descendingly - most important when we're
// showing records in reverse order!
$sql = '
    SELECT
        country
    FROM
        countries
    ORDER BY
        country DESC
    LIMIT
        ' . (($pagination->get_pages() - $pagination->get_page()) * $records_per_page) . ', ' . $records_per_page . '
';

// run the query
mysqli_query($connection. $sql) or die(mysqli_error($connection));

?>

<table>
    <thead>
    <tr>
        <th>Country</th>
    </tr>
    </thead>
    <tbody>
    <?php $index = 0; while ($row = mysqli_fetch_assoc($result)): ?>
    <tr<?php echo $index++ % 2 ? ' class="even"' : ''; ?>>
        <td><?php echo $row['country']; ?></td>
    </tr>
    <?php endwhile; ?>
    </tbody>
</table>

<?php

// render the pagination links
$pagination->render();

结果将类似于

Zebra_Pagination