benjaminmedia / zebra-pagination
一个通用的、兼容 Twitter Bootstrap 的 PHP 分页库,可自动生成导航链接
Requires
- php: >=5
README
Zebra_Pagination
一个通用的、兼容 Twitter Bootstrap 的分页库,可自动生成导航链接
一个通用的、Twitter Bootstrap 兼容的分页脚本,根据总记录数和每页显示的记录数自动生成导航链接以及下一页/上一页链接。它可以将大量数据分成更小的块,减少网络流量,同时提高可读性、美观性和可用性。
遵循分页最佳实践(提供大可点击区域,不使用下划线,选中的页面清晰突出,页面链接间隔适中,提供“上一页”和“下一页”链接,提供“首页”和“末页”链接——如 2007 年 Faruk Ates 的文章中概述的那样,现在可在 这里 找到,可以生成自然顺序或反向顺序的链接,易于本地化,支持不同位置的下一页/上一页按钮,支持通过 GET 或 URL 重写进行页面传播,SEO 友好,并且可以通过 CSS 轻松自定义外观)。
请注意,这是一个 通用 分页脚本,这意味着它不显示任何记录,并且不依赖于数据库连接或 SQL 查询,因此非常灵活!开发者需要根据此分页脚本返回的信息获取实际数据并根据这些信息进行显示。优点是它可以用于从任何来源(如数组或数据库)分页记录。
代码注释丰富,当 PHP 的错误报告级别设置为 E_ALL 时,不会生成警告/错误/通知。
📚 文档
支持此库的开发
功能
- 这是一个通用库:可用于从数组或数据库分页记录
- 它自动生成导航链接,给定总项目数和每页项目数(也包含最佳实践示例)
- 导航链接可以按自然顺序或反向顺序生成
- 它是 SEO 友好的——它使用 rel="next" 和 rel="prev",并解决了没有导航的首页和 URL 中包含页码的首页上的重复内容问题
- 外观可以通过 CSS 轻松自定义
- 与 Twitter Bootstrap 兼容
- 代码注释丰富,当 PHP 的错误报告级别设置为 E_ALL 时,不会生成警告/错误/通知
- 有 出色的文档
要求
PHP 5+
安装
下载最新版本,解压缩,并在项目中加载
require_once ('Zebra_Pagination.php');
使用 Composer 安装
您可以通过 Composer 安装 Zebra_Pagination
composer require stefangabos/zebra_pagination
如何使用
确保在页面底部有
<!-- 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 type="text/javascript" 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 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> <tr><th>Country</th></tr> <?php foreach ($countries as $index => $country):?> <tr<?php echo $index % 2 ? ' class="even"' : ''); ?>> <td><?php echo $country; ?></td> </tr> <?php endforeach; ?> </table> <?php // render the pagination links $pagination->render();
将导致类似的结果
您可以使用navigation_position()方法将导航链接的位置设置为分页链接的左侧或右侧
$pagination->navigation_position('left');
$pagination->navigation_position('right');
可以通过labels()方法更改“上一页”和“下一页”链接的标签
$pagination->labels('Previous', 'Next');
从MySQL中分页数据
<?php // how many records should be displayed on a page? $records_per_page = 10; // include the pagination class require 'path/to/Zebra_Pagination.php'; // instantiate the pagination object $pagination = new Zebra_Pagination(); // the MySQL statement to fetch the rows // note how we build the LIMIT // also, note the "SQL_CALC_FOUND_ROWS" // this is to get the number of rows that would've been returned if there was no LIMIT // see https://dev.mysqlserver.cn/doc/refman/5.0/en/information-functions.html#function_found-rows $sql = ' SELECT SQL_CALC_FOUND_ROWS 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 = mysql_query($sql))) or die(mysql_error()); // fetch the total number of records in the table $rows = mysql_fetch_assoc(mysql_query('SELECT FOUND_ROWS() AS rows')); // 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> <tr><th>Country</th></tr> <?php $index = 0; while ($row = mysql_fetch_assoc($result)):?> <tr<?php echo $index++ % 2 ? ' class="even"' : ''; ?>> <td><?php echo $row['country']; ?></td> </tr> <?php endwhile; ?> </table> <?php // render the pagination links $pagination->render();
以逆序从MySQL中分页数据
<?php // how many records should be displayed on a page? $records_per_page = 10; // include the pagination class 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 = mysql_query('SELECT COUNT(id) AS records FROM countries'))) or die (mysql_error()); // pass the total number of records to the pagination class $pagination->records(array_pop(mysql_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 mysql_query($sql) or die(mysql_error()); ?> <table> <tr><th>Country</th></tr> <?php $index = 0; while ($row = mysql_fetch_assoc($result)): ?> <tr<?php echo $index++ % 2 ? ' class="even"' : ''; ?>> <td><?php echo $row['country']; ?></td> </tr> <?php endwhile; ?> </table> <?php // render the pagination links $pagination->render();
将导致类似的结果
📚 检查出色的文档!