-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathcursor-pagination.php
More file actions
31 lines (24 loc) · 1003 Bytes
/
cursor-pagination.php
File metadata and controls
31 lines (24 loc) · 1003 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
use Doctrine\ORM\Tools\CursorPagination\CursorPaginator;
$cursor = $_GET['cursor'] ?? null;
$query = $entityManager->createQuery('SELECT p FROM BlogPost p ORDER BY p.createdAt DESC, p.id DESC');
/** @var CursorPaginator<BlogPost> $paginator */
$paginator = (new CursorPaginator($query))
->paginate(cursor: $cursor, limit: 15);
?>
<p><?= $paginator->getTotalCount() ?> result(s) in total, <?= $paginator->countPageItems() ?> on this page.</p>
<ul>
<?php foreach ($paginator as $post): ?>
<li><?= escape($post->getTitle()) ?></li>
<?php endforeach ?>
</ul>
<?php if ($paginator->hasToPaginate()): ?>
<nav>
<?php if ($paginator->hasPreviousPage()): ?>
<a href="?cursor=<?= escape($paginator->getPreviousCursorAsString()) ?>">Previous</a>
<?php endif ?>
<?php if ($paginator->hasNextPage()): ?>
<a href="?cursor=<?= escape($paginator->getNextCursorAsString()) ?>">Next</a>
<?php endif ?>
</nav>
<?php endif ?>