All Projects → romanzipp → PHP-Slim-Pagination

romanzipp / PHP-Slim-Pagination

Licence: other
Example pagination for slimphp/Slim

PHP Slim Pagination

Example pagination for slimphp/Slim. No extra classes needed.

In this exmaple I used the PHP Slim framework with Twig view templates. As fronted framework Semantic-UI with the Pagination Module is used. The shown scenarion is listing Blog posts.

Route (index.php)

$app->get('/posts', function(Request $req,  Response $res, $args = []) use ($cache) {

    $page      = ($req->getParam('page', 0) > 0) ? $req->getParam('page') : 1;
    $limit     = 5; // Number of posts on one page
    $skip      = ($page - 1) * $limit;
    $count     = Post::getCount([]); // Count of all available posts

    return $this->view->render($res, 'post-list.twig', [
        'pagination'    => [
            'needed'        => $count > $limit,
            'count'         => $count,
            'page'          => $page,
            'lastpage'      => (ceil($count / $limit) == 0 ? 1 : ceil($count / $limit)),
            'limit'         => $limit,
        ],
        // return list of Posts with Limit and Skip arguments
        'posts'         => Post::getList([
            'limit'         => $limit,
            'skip'          => $skip,
        ])
    ]);
});

Template View (Twig used)

{% if pagination.needed %}
    <div class="ui pagination menu">
        {% for i in 1..pagination.lastpage %}
            <a class="{% if i == pagination.page %}active{% endif %} item" href="?page={{ i }}">{{ i }}</a>
        {% endfor %}
    </div>
{% endif %}

<div class="ui container">
    {% for post in posts %}
        <a class="item">
        	{# Post contents (title, url, ...) #}
        </a>
    {% endfor %}
</div>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].