s-damian/damian-pagination-php

简单的分页 PHP 库

v1.0.20 2024-08-17 08:41 UTC

README

PHP 分页库 - 开源

Tests Static analysis Latest Stable Version License

Damian Pagination PHP - 开源分页

简介 - Damian Pagination PHP 包

Damian Pagination PHP 是一个开源 PHP 库,允许您生成简单且完整的 分页

此分页适用于所有 PHP 项目(带有 MVC、不带 MVC 等)。

此分页与 Bootstrap 5 CSS 兼容。

无限制地轻松分页!

<?php

$pagination = new Pagination();

$pagination->paginate($countElements);

$limit = $pagination->getLimit();
$offset = $pagination->getOffset();

echo $pagination->render();
echo $pagination->perPageForm();

作者

此包由 Stephen Damian 开发

要求

  • PHP 8.0 || 8.1 || 8.2 || 8.3

摘要

简介

此开源分页包含 PHP 文件和一个 CSS 样式表。

CSS 样式表的示例位于 vendor/s-damian/damian-pagination-php/src/css 目录。您可以根据需要编辑它们。

此分页还允许您生成 每页 分页。这将生成一个带有可点击选项的表单 HTML 标签和选择 HTML 标签。

安装

使用 Composer 安装

composer require s-damian/damian-pagination-php

不使用 Composer 安装

如果您不使用 Composer 安装此包,您在使用此包之前必须手动“require”。例如

<?php

require_once './your-path/damian-pagination-php/src/DamianPaginationPhp/bootstrap/load.php';

分页实例方法

示例

简单示例

<?php

$pagination = new Pagination();

$pagination->paginate($countElements);

$limit = $pagination->getLimit();
$offset = $pagination->getOffset();

// Here your SQL query with $limit and $offset

// Then your listing of elements with a loop

echo $pagination->render();
echo $pagination->perPageForm();

使用 Bootstrap 5 渲染分页的示例

Laravel Man Pagination

使用 SQL 查询的示例

<?php

use DamianPaginationPhp\Pagination;

// Count articles in DB
function countArticles(): int
{
    $sql = "SELECT COUNT(*) AS nb FROM articles";
    $query = db()->query($sql);
    $result = $query->fetch();
    
    return $result->nb;
}

// Collect articles from DB
function findArticles($limit, $offset)
{
    $sql = "SELECT * FROM articles LIMIT ? OFFSET ?";
    $query = db()->prepare($sql);
    $query->bindValue(1, $limit, PDO::PARAM_INT);
    $query->bindValue(2, $offset, PDO::PARAM_INT);
    $query->execute();

    return $query;
}

// Creating an object Pagination
$pagination = new Pagination();

// Paginate
$pagination->paginate(countArticles());

$limit = $pagination->getLimit();
$offset = $pagination->getOffset();

$articles = findArticles($limit, $offset);

// Show elements one by one that are retrieved from the database
foreach ($articles as $article) {
    echo htmlspecialchars($article->title);
}

// Show the Pagination
echo $pagination->render();
// Show the per page
echo $pagination->perPageForm();

函数 db() 是数据库连接的结果(例如 PDO 实例)。

根据您的需要,您也可以使用此库与您最喜欢的 ORM 一起使用。

目录文件的列表示例

<?php

use DamianPaginationPhp\Pagination;

$scandir = scandir('your_path_upload');

$listFilesFromPath = [];
$count = 0;
foreach ($scandir as $f) {
    if ($f !== '.' && $f !== '..') {
        $listFilesFromPath[] = $f;
        $count++;
    }
}

// Creating an object Pagination
$pagination = new Pagination();

// Paginate
$pagination->paginate($count);

$limit = $pagination->getLimit();
$offset = $pagination->getOffset();

// Listing
$files = array_slice($listFilesFromPath, $offset, $limit);

// Show files one by one
foreach ($files as $file) {
    echo $file;
}

// Show the Pagination
echo $pagination->render();
// Show the per page
echo $pagination->perPageForm();

向实例添加参数

<?php

// To change number of Elements per page:
$pagination = new Pagination(['pp' => 50]);
// Is 15 by default

// To change number of links alongside the current page:
$pagination = new Pagination(['number_links' => 10]);
// Is 5 by default

// To change the choice to select potentially generate with perPageForm():
$pagination = new Pagination(['options_select' => [5, 10, 50, 100, 500, 'all']]);
// The value of 'options_select' must be a array.
// Only integers and 'all' are permitted.
// Options are [15, 30, 50, 100, 200, 300] by default.

// To change the CSS style of the pagination (to another CSS class as default):
$pagination = new Pagination(['css_class_p' => 'name-css-class-of-pagintion']);
// The CSS class name is by default "pagination".

// To change the CSS style of the pagination active (to another CSS class as default):
$pagination = new Pagination(['css_class_link_active' => 'name-css-class-of-pagintion']);
// The active CSS class name is by default "active".

// To change the CSS style of a per page (select) (to another id id as default):
$pagination = new Pagination(['css_id_pp' => 'name-css-id-of-per-page-form']);
// The CSS ID name is by default  "per-page-form".

// To use Bootstrap CSS:
$pagination = new Pagination(['css_class_p' => 'pagination']);
// The CSS class name is by default "block-pagination"
// We must put "pagination"

自定义配置

您可以更改默认语言。默认为英语(en)。

支持的语言:“cn”、“de”、“ee”、“en”、“es”、“fr”、“it”、“jp”、“pt”、“ru”。

设置默认语言

<?php

use DamianPaginationPhp\Config\Config;

// Change the language to "fr" (it's "en" by default.
Config::set(["lang" => "fr"]);

支持

错误和安全漏洞

如果您发现错误或安全漏洞,请向 Stephen 发送消息。谢谢。

将及时解决所有错误和安全漏洞。

许可证

此项目是 MIT 许可证下的开源包。有关详细信息,请参阅 LICENSE 文件。