Pagination Component

Creates customizable pagination and pager links. Limits and offsets arrays and database queries. Built-in styles for Bootstrap, Zurb Foundation, Semantic UI, Materialize, and UIkit.

use BootPress\Pagination\Component as Pagination;

Packagist License MIT HHVM Tested PHP 7 Supported Build Status Code Climate Test Coverage


Creates customizable pagination and pager links. Limits and offsets arrays and database queries. Built-in styles for Bootstrap, Zurb Foundation, Semantic UI, Materialize, and UIkit.

public __construct ( [ string $framework = 'bootstrap' ] )

@param $framework

You can indicate your preference now, or later in $this->html().

use BootPress\Pagination\Component as Paginator;

$pagination = new Paginator;

public mixed __get ( string $name )

Access the following information:

  • 'pager' - (array) The current HTML pager styles.
  • 'links' - (array) The current HTML pagination styles.
  • 'offset' - (int) How much to offset the records, starting at 0.
  • 'length' - (int) The total number of records to display.
    • 'offset' and 'length' are meant to be compatible with array_slice().
  • 'limit' - (string) A string to add onto the end of your query eg. " LIMIT {$offset}, {$length}".
    • If no limit is needed, then this will be an empty string.
  • 'last_page' - (bool) Whether or not this is the last page being looked at.
  • 'current_page' - (int) The current page number being viewed, starting at 1.
  • 'number_pages' - (int) The total number of pages, starting at 1.
  • 'previous_url' - (string|null) The previous page url, if applicable.
  • 'next_url' - (string|null) The next page url, if applicable.
@example
$records = range(1, 100);
if (!$pagination->set()) {
    $pagination->total(count($records));
}
$display = array_slice($records, $pagination->offset, $pagination->length);
echo implode(',', $display); // 1,2,3,4,5,6,7,8,9,10

public bool set ( [ string $page = 'page' [, int $limit = 10 [, string $url = null ]]] )

Check if we need a total count. There's no sense in querying the database if you don't have to. Plus, you have to call this anyways to set things up.

@param $page

The url query parameter that pertains these records.

@param $limit

How many records to return at a time.

@param $url

The url to use. The default is the current url.

@example
if (!$pagination->set()) {
    $pagination->total(100);
}

public total ( int $count )

Let us know how many records we're working with. Check if $this->set() already before telling us to save yourself a query.

public html ( [ string $type = 'bootstrap' [, array $options ]] )

Customize the pagination and pager links.

@param $type

Either the CSS framework you want to use ('bootstrap', 'zurb_foundation', 'semantic_ui', 'materialize', 'uikit'), or what you want to override (the pagination 'links', or 'pager' html).

@param $options

The values you want to override. The default (bootstrap) values are:

  • If $type == 'links'
    • 'wrapper' => '<ul class="pagination">{{ value }}</ul>',
    • 'link' => '<li><a href="{{ url }}">{{ value }}</a></li>',
    • 'active' => '<li class="active"><span>{{ value }}</span></li>',
    • 'disabled' => '<li class="disabled"><span>{{ value }}</span></li>',
    • 'previous' => '&laquo;', // Setting to null will remove this entirely
    • 'next' => '&raquo;', // Setting to null will remove this entirely
    • 'dots' => '&hellip;', // Setting to null will remove this entirely
  • If $type == 'pager'
    • 'wrapper' => '<ul class="pager">{{ value }}</ul>',
    • 'previous' => '<li class="previous"><a href="{{ url }}">&laquo; {{ value }}</a></li>',
    • 'next' => '<li class="next"><a href="{{ url }}">{{ value }} &raquo;</a></li>',

Put '{{ value }}' and '{{ url }}' where you want those to go. If you set anything to null, then only the '{{ value }}' will be returned.

@example
$pagination->html('links', array(
    'wrapper' => '<ul class="pagination pagination-sm">{{ value }}</ul>',
));

public string links ( [ int $pad = 3 ] )

Display pagination links.

@param $pad

The number of neighboring links you would like to be displayed to the right, and to the left of the currently active link. We fudge this number at times for there to be a consistent total number of links throughout.

public string pager ( [ string|array $previous = 'Previous' [, string|array $next = 'Next' ]] )

Display pager links. If you pass $previous and $next arrays, then there is no need to $this->set() anything.

@param $previous

A prompt (string) for the previous page, or an array('url'=>'', 'title'=>'') to pass the values directly.

@param $next

A prompt (string) for the next page, or an array('url'=>'', 'title'=>'') to pass the values directly.

Document Your Code

Installation

Add the following to your composer.json file.

{
    "require ": {
        "bootpress/pagination": "^1.0"
    }
}

Example Usage

<?php

use BootPress\Pagination\Component as Paginator;

$pagination = new Paginator;

// Paginate an array
$records = range(1, 100);
if (!$pagination->set('page', 10, 'http://example.com')) {
    $pagination->total(count($records));
}
$display = array_slice($records, $pagination->offset, $pagination->length);
echo implode(',', $display); // 1,2,3,4,5,6,7,8,9,10

// Generate pagination links
echo $pagination->links();
/*
<ul class="pagination">
    <li class="active"><span>1</span></li>
    <li><a href="http://example.com?page=2of10">2</a></li>
    <li><a href="http://example.com?page=3of10">3</a></li>
    <li><a href="http://example.com?page=4of10">4</a></li>
    <li><a href="http://example.com?page=5of10">5</a></li>
    <li><a href="http://example.com?page=6of10">6</a></li>
    <li><a href="http://example.com?page=7of10">7</a></li>
    <li class="disabled"><span>&hellip;</span></li>
    <li><a href="http://example.com?page=10of10">10</a></li>
    <li><a href="http://example.com?page=2of10">&raquo;</a></li>
</ul>
*/

// And a pager for good measure
echo $pagination->pager();
/*
<ul class="pager">
    <li class="next"><a href="http://example.com?page=2of10">Next &raquo;</a></li>
</ul>
*/