klab / php-simple-pagination
Simple Pagination Class for PHP
v2.0.0
2019-12-06 08:32 UTC
Requires
- php: >=7.1
Requires (Dev)
- phpstan/phpstan: ^0.12.0
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-10-25 21:21:56 UTC
README
install
composer require klab/php-simple-pagination
example
<?php require_once __DIR__ . '/SimplePagination.php'; $db = [ 1 => ['id' => 1], 2 => ['id' => 2], 3 => ['id' => 3], 4 => ['id' => 4], 5 => ['id' => 5], 6 => ['id' => 6], 7 => ['id' => 7], 8 => ['id' => 8], 9 => ['id' => 9], ]; $items = []; $current = (int)$_GET['page']; $count = 3; if ($current === 1) { $items = [ $db[1], $db[2], $db[3], $db[4], ]; } else if ($current === 2) { $items = [ $db[4], $db[5], $db[6], $db[7], ]; } else if ($current === 3) { $items = [ $db[7], $db[8], $db[9], ]; } else { $current = 1; $items = [ $db[1], $db[2], $db[3], $db[4], ]; } $pagination = new SimplePagination($current, $count); $pagination->checkLastPage($items); ?> <html> <body> <h1>SimplePagination Example</h1> <?php if($pagination->current > 1): ?> <a href='?page=<?php echo $pagination->prev ?>'>Previous</a> <?php else: ?> Previous <?php endif ?> <?php foreach ($items as $item): ?> <a href="<?php echo $item['id'] ?>"><?php echo $item['id'] ?></a> <?php endforeach ?> <?php if(!$pagination->is_last_page): ?> <a href='?page=<?php echo $pagination->next ?>'>Next</a> <?php else: ?> Next <?php endif ?> </body> </html>