criativamos/paginator

一个简单的PHP库,用于创建分页

1.0.5 2017-02-11 15:16 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:03:58 UTC


README

一个简单的PHP库,用于创建分页。

安装

$ composer require criativamos/paginator

如何使用

您可以在 example.php 中看到完整的实现。不要忘记创建测试数据库 mock_data.sql

$query = "SELECT * FROM mock_data"
//the first parameter is a PDO instance with database connection
//the third parameter is the number of items per page. The default value is 15
$pg = new \Criativamos\Paginator\Paginator($pdoconnection, $query, 10);
//print result
if($pg->rowCount() > 0){
    foreach ($pg->getData() as $data){
        echo '<tr>';
        echo '<td>'.$data->id.'</td>';
        echo '<td>'.$data->first_name.'</td>';
        echo '<td>'.$data->last_name.'</td>';
        echo '<td>'.$data->email.'</td>';
        echo '</tr>';
    }
}
...
//pagination
$pg->render();

带参数的查询

$query = "SELECT * FROM mock_data WHERE LOWER (first_name) LIKE :SEARCH";
$pg = new \Criativamos\Paginator\Paginator($pdoconnection, $query);
$pg->setParameters([
    ':SEARCH' => '%jo%'
]);

您也可以这样做

$query = "SELECT * FROM mock_data WHERE LOWER (first_name) LIKE :SEARCH";
$pg = new \Criativamos\Paginator\Paginator($pdoconnection);
$pg->setQuery($query, [
    ':SEARCH' => '%jo%'
]);

获取详细结果

$query = "SELECT * FROM mock_data"
$pg = new \Criativamos\Paginator\Paginator($pdoconnection, $query);
print_r( $pg->results() );

输出

Array
(
    [currentPage] => 1
    [total] => 200
    [perPage] => 10
    [lastPage] => 19
    [nextPageUrl] => https:///lab/pagination/example.php?page=2
    [prevPageUrl] => https:///lab/pagination/example.php
    [currentUrl] => https:///lab/pagination/example.php
    [from] => 1
    [to] => 10
    [data] => Array
        (
            ...
        )
)