Controller代码:
<?php
// Current page to show
// In a controller this can be:
// $this->request->getQuery('page', 'int'); // GET
// $this->request->getPost('page', 'int'); // POST
$currentPage = (int) $_GET["page"];
// The data set to paginate
$robots = Robots::find();
// Create a Model paginator, show 10 rows by page starting from $currentPage
$paginator = new \Phalcon\Paginator\Adapter\Model(
array(
"data" => $robots,
"limit"=> 10,
"page" => $currentPage
)
);
// Get the paginated results
$page = $paginator->getPaginate();
View代码:
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Type</th>
</tr>
<?php foreach ($page->items as $item) { ?>
<tr>
<td><?php echo $item->id; ?></td>
<td><?php echo $item->name; ?></td>
<td><?php echo $item->type; ?></td>
</tr>
<?php } ?>
</table>
<a href="/robots/search">First</a>
<a href="/robots/search?page=<?= $page->before; ?>">Previous</a>
<a href="/robots/search?page=<?= $page->next; ?>">Next</a>
<a href="/robots/search?page=<?= $page->last; ?>">Last</a>
<?php echo "You are in page ", $page->current, " of ", $page->total_pages; ?>
\Phalcon\Paginator\Adapter\Model 属性:
Attribute | Description |
---|---|
items | The set of records to be displayed at the current page |
current | The current page |
before | The previous page to the current one |
next | The next page to the current one |
last | The last page in the set of records |
total_pages | The number of pages |
total_items | The number of items in the source data |