spagination/sprint-framework-pagination-package

这是一个简单小巧的单类PHP分页,它根据HTTP请求处理分页。

1.0.0 2022-06-21 19:53 UTC

This package is auto-updated.

Last update: 2024-09-24 19:00:17 UTC


README

这个库是sprint框架包的一部分

版本 1.0.0

嘿!这是一个简单小巧的单类PHP分页,它根据HTTP请求处理分页。所有属性和方法都是静态的。代码库非常小,易于理解。因此,你可以将其用作更复杂分页的样板。

包含文件的安装

// Require the class
include 'src\SPagination.php';

使用Composer安装

只需运行 composer require spagination/sprint-framework-pagination-package 然后将自动加载器添加到你的项目中,如下所示

// Autoload files using composer
require_once __DIR__ . '/vendor/autoload.php';

// Use this namespace
use sprint\spagination\SPagination;

		// Create an instance of the SPagination class and pass the current page retrieved via http request and the number of results per page as parameters to the constructor
		//First parameter is the page the current page number and second is the limit per page
		$spagination = new SPagination(intval($_GET['page']), 5);

// sql query offset, dynamically generated via the number of results to display per page and the current page number
		//echo $spagination::$offset;

// How to use with the database (This example consider the mysqli, but you can use PDO or any other drivers as well)
// First connect to your database
		$conn   = mysqli_connect("localhost","root","password","database");

//Select the table, use $spagination::$limit to get number of result to return and $spagination::$offset to get the offset dynamically
//To get the total filtered rows use SQL_CALC_FOUND_ROWS command in your query this will ignored the limit and run the query as no limit was informed
		$query  = mysqli_query($conn, "SELECT SQL_CALC_FOUND_ROWS * FROM table ORDER BY id ASC LIMIT ".$spagination::$limit." OFFSET ".$spagination::$offset."");
		$results = array();

// Total filtered results from the sql query
		$total = mysqli_fetch_assoc(mysqli_query($conn, "SELECT FOUND_ROWS() AS totalFiltered"));
		$spagination::$total = $total["totalFiltered"];

//Now fetch the data and store in array
		while($row = mysqli_fetch_assoc($query)){
			$results[] = $row;
		}

// Either show dots in the pagination or not(Accept boolean value)
		$spagination::$dots = true;//false

//debug the database results with the pagination
		echo "<pre>";
			var_dump($results);
		echo "</pre>";
// Build the pagination
		echo $spagination::page();

自定义分页样式

SPagination使用Bootstrap作为默认样式,但你可以通过定义元素的类来创建自己的自定义样式。如果你想在分页中使用Bootstrap进行样式设计,请参考Bootstrap文档以获取更多信息。

//This will add custom class to anchor element
//Add general class to the class key and active class to the active key
$spagination::$classes["a"] = array(
	"class" => [""] //default class: ["page-link"]
	"active" => [""] //default class: ["active", "disabled"]
);

//This will add custom class to list item element
//Add general class to the class key and active class to the active key (this is used in case you use list item to style active behaviour)
$spagination::$classes["li"] = array(
	"class" => [""] //default class: ["page-item"]
	"active" => [""]
);

//This will add custom class to unordered list element
//Add general class to the class key
$spagination::$classes["ul"] = array(
	"class" => [""] //default class: ["pagination"]
);

许可证

本项目采用MIT许可证。有关更多信息,请参阅许可证文件

贡献者