Skip to content
This repository has been archived by the owner on Feb 6, 2018. It is now read-only.

romanzipp/PHP-Slim-Pagination

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 

Repository files navigation

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>

About

Example pagination for slimphp/Slim

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published