Skip to content

Instantly share code, notes, and snippets.

@queued
Created August 1, 2018 14:54
Show Gist options
  • Select an option

  • Save queued/1abbfb03df2f72d5074710a13bca2ea3 to your computer and use it in GitHub Desktop.

Select an option

Save queued/1abbfb03df2f72d5074710a13bca2ea3 to your computer and use it in GitHub Desktop.

Revisions

  1. queued created this gist Aug 1, 2018.
    50 changes: 50 additions & 0 deletions PAGINATE.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    # PHP Slim Pagination
    Example pagination for [slimphp/Slim](https://github.com/slimphp/Slim). No extra classes needed.

    In this exmaple I used the [PHP Slim](https://github.com/slimphp/Slim) framework with Twig view templates. As fronted framework [Semantic-UI](https://github.com/Semantic-Org/Semantic-UI) with the [Pagination Module](http://semantic-ui.com/collections/menu.html#pagination) is used.
    The shown scenarion is listing Blog posts.

    ## Route (index.php)
    ```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)
    ```twig
    {% 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>
    ```