$numItems = $db->fetchColumn('SELECT COUNT(*) FROM table');
$numItemsPerPage = 10;
$curPage = max(1, (int) $request->query->get('p'));
$numPages = ceil($numItems / $numItemsPerPage);
$curPage
and $numitemsPerPage
to your repository and add it use it in a LIMIT
clause
public function findAll($curPage = 1, $numItemsPerPage = 10) {
return $this->db->fetchAll('
SELECT * FROM datasets
ORDER BY id DESC
LIMIT ' . (int) (($curPage - 1) * $numItemsPerPage) . ',' .
(int) ($numItemsPerPage)
);
}
$curPage; // To indicate the active page
$numItems; // To indicate the number of items
$baseUrl = 'http://example.dev/news/'; // to prepend to the pagination params
$pagination = array(1,2,'...',6,7,8,9,10); // Subset of pages to show
{% set prevPage = curPage - 1 %}
{% set nextPage = curPage + 1 %}
{% if numPages > 1 %}
<div class="text-center">
<ul class="pagination pagination-sm">
{% if curPage > 1 %}
<li class="previousPage"><a href="{{ baseUrl }}?p={{ prevPage }}" data-p="{{ prevPage }}">< Previous</a></li>
{% else %}
<li class="previousPage disabled"><span>< Previous</span></li>
{% endif %}
{% for page in pagination %}
<li {% if page == curPage %} class="active"{% endif %}{% if page == '..' %} class="disabled"{% endif %}>
{% if page == curPage %}
<span>{{ page }}</span>
{% elseif page == '..' %}
<span>…</span>
{% else %}
<a href="{{ baseUrl }}?p={{ page }}" data-p="{{ page }}">{{ page }}</a>
{% endif %}
</li>
{% endfor %}
{% if curPage < numPages %}
<li class="nextPage"><a href="{{ baseUrl }}?p={{ nextPage }}" data-p="{{ nextPage }}">Next ></a></li>
{% else %}
<li class="nextPage disabled"><span>Next ></span></li>
{% endif %}
</ul>
<p class="text-muted">
<em>{{ numItems }} items</em>
</p>
</div>
{% endif %}
<?php
// Get pagination params
$numItemsPerPage = 10;
$curPage = max(1, (int) $request->query->get('p'));
$numPages = ceil($app['datasets']->numDatasets() / $numItemsPerPage);
// Get datasets (for current page)
$datasets = $app['datasets']->findAll(
$curPage,
$numItemsPerPage
);
// Build pagination
$pagination = new \Taku\Pagination\Pagination(
$app['url_generator']->generate('datasets.overview'), // = baseUrl
$curPage,
$numPages
);
// Render our template
return $app['twig']->render('datasets/overview.twig', array(
'datasets' => $datasets,
'pagination' => $pagination->getHtml()
));
$_GET
params
$_SESSION
params
public function forceFilter(Application $app) {
if ($app['session']->get('filter_products') == null) {
$app['session']->set('filter_products', array(
'title' => '',
'type' => '',
'brand' => ''
));
}
}
public function getFilter(Application $app) {
return $app['session']->get('filter_products');
}
$filterform = $app['form.factory']
->createNamed('filterform', 'form', $this->getFilter($app))
->...
public function setFilter(Application $app, $filter) {
$app['session']->set('filter_products', $filter);
}
if ('POST' == $app['request']->getMethod()) {
$filterform->handleRequest($app['request']);
if ($filterform->isValid()) {
$this->setFilter($app, $filterform->getData());
// return $app->redirect('...'); // Not required
}
}
$products = $app['db.products']->findFiltered(
$this->getFilter($app), $curPage, $numItemsPerPage
);
public function findFiltered($filter, $curPage = 1, $numItemsPerPage = 10) {
// @TODO: some magic here?!
return $this->db->fetchAll('
SELECT * FROM products
WHERE products.deleted = ?
ORDER BY id DESC
LIMIT ' . (int) (($curPage - 1) * $numItemsPerPage) . ',' .
(int) ($numItemsPerPage),
array('N')
);
}
where
clause partjoin
withpublic function findFiltered($filter, $curPage = 1, $numItemsPerPage = 10) {
$extraJoins = '';
$extraWhere = '';
// Title set via Filter
if ($filter['title'] != '') {
$extraWhere .= ' AND products.title LIKE ' . $this->db->quote('%'.$filter['title'].'%', \PDO::PARAM_STR);
}
// Type set via Filter
if ($filter['type'] != '') {
$extraJoins .= ' INNER JOIN product_types ON products.type_id = product_types.id';
$extraWhere .= ' AND products.type_id = ' . $this->db->quote($filter['type'], \PDO::PARAM_INT);
}
// Brand set via filter
if ($filter['brand'] != '') {
$extraJoins .= ' INNER JOIN brands ON products.brand_id = brands.id';
$extraWhere .= ' AND products.brand_id = ' . $this->db->quote($filter['brand'], \PDO::PARAM_INT);
}
return $this->db->fetchAll('
SELECT * FROM products' . $extraJoins . '
WHERE products.deleted = ?' . $extraWhere . '
ORDER BY id DESC
LIMIT ' . (int) (($curPage - 1) * $numItemsPerPage) . ',' .
(int) ($numItemsPerPage),
array('N')
);
}
$numItems
!)$extraJoins
and $extraWhere
parts to make it reusable