定制 benjaminmedia/zebra-pagination 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

benjaminmedia/zebra-pagination

Composer 安装命令:

composer require benjaminmedia/zebra-pagination

包简介

A generic, Twitter Bootsrap compatible, PHP pagination library that automatically generates navigation links

README 文档

README

zebrajs

Zebra_Pagination

A generic, Twitter Bootstrap compatible, pagination library that automatically generates navigation links

Latest Stable Version Total Downloads Monthly Downloads Daily Downloads License

A generic, Twitter Bootstrap compatible, pagination script that automatically generates navigation links as well as next/previous page links, given the total number of records and the number of records to be shown per page. Useful for breaking large sets of data into smaller chunks, reducing network traffic and, at the same time, improving readability, aesthetics and usability.

Adheres to pagination best practices (provides large clickable areas, doesn't use underlines, the selected page is clearly highlighted, page links are spaced out, provides "previous page" and "next page" links, provides "first page" and "last page" links - as outlined in an article by Faruk Ates from 2007, which can now be found here, can generate links both in natural as well as in reverse order, can be easily, localized, supports different positions for next/previous page buttons, supports page propagation via GET or via URL rewriting, is SEO-friendly, and the appearance is easily customizable through CSS.

Please note that this is a generic pagination script, meaning that it does not display any records and it does not have any dependencies on database connections or SQL queries, making it very flexible! It is up to the developer to fetch the actual data and display it based on the information returned by this pagination script. The advantage is that it can be used to paginate over records coming from any source like arrays or databases.

The code is heavily commented and generates no warnings/errors/notices when PHP's error reporting level is set to E_ALL.

📚 Documentation

Support the development of this library

Donate

Features

  • it is a generic library: can be used to paginate records both from an array or from a database
  • it automatically generates navigation links, given the total number of items and the number of items per page (examples of best practices are also included)
  • navigation links can be generated in natural or in reverse order
  • it is SEO-friendly – it uses rel="next" and rel="prev" and solves the problem of duplicate content on the first page without navigation and the first page having the page number in the URL
  • appearance is easily customizable through CSS
  • compatible with Twitter Bootstrap
  • code is heavily commented and generates no warnings/errors/notices when PHP’s error reporting level is set to E_ALL
  • has awesome documentation

Requirements

PHP 5+

Installation

Download the latest version, unpack it, and load it in your project

require_once ('Zebra_Pagination.php');

Installation with Composer

You can install Zebra_Pagination via Composer

composer require stefangabos/zebra_pagination

How to use

Make sure that in the of your page you have

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

If you want to preserve hashes in the URL, also include the JavaScript file – simply including it will suffice; (jQuery needs to also be loaded before loading this file)

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

Paginate data from an array:

The 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();

Would result is something like

Zebra_Pagination

You can set the navigation links' position to the left or to the right of the pagination links using the navigation_position() method:

$pagination->navigation_position('left');

Zebra_Pagination

$pagination->navigation_position('right');

Zebra_Pagination

Labels for "Previous" and "Next" links can be changed with the labels() method:

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

Zebra_Pagination

Paginate data from 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 http://dev.mysql.com/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();

Paginate data from MySQL in reverse order:

<?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();

Would result in something like

Zebra_Pagination

📚 Checkout the awesome documentation!

benjaminmedia/zebra-pagination 适用场景与选型建议

benjaminmedia/zebra-pagination 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.3k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「paging」 「paginator」 「pagination」 「paginate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 benjaminmedia/zebra-pagination 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 benjaminmedia/zebra-pagination 我们能提供哪些服务?
定制开发 / 二次开发

基于 benjaminmedia/zebra-pagination 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 8
  • Forks: 74
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0
  • 更新时间: 2017-06-13