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.
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.
You can indicate your preference now, or later in $this->html()
.
use BootPress\Pagination\Component as Paginator;
$pagination = new Paginator;
Access the following information:
array_slice()
." LIMIT {$offset}, {$length}"
.
$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
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.
The url query parameter that pertains these records.
How many records to return at a time.
The url to use. The default is the current url.
if (!$pagination->set()) {
$pagination->total(100);
}
Let us know how many records we're working with. Check if $this->set()
already before telling us to save yourself a query.
Customize the pagination and pager links.
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).
The values you want to override. The default (bootstrap) values are:
$type == 'links'
'<ul class="pagination">{{ value }}</ul>'
,'<li><a href="{{ url }}">{{ value }}</a></li>'
,'<li class="active"><span>{{ value }}</span></li>'
,'<li class="disabled"><span>{{ value }}</span></li>'
,'«'
, // Setting to null
will remove this entirely'»'
, // Setting to null
will remove this entirely'…'
, // Setting to null
will remove this entirely$type == 'pager'
'<ul class="pager">{{ value }}</ul>'
,'<li class="previous"><a href="{{ url }}">« {{ value }}</a></li>'
,'<li class="next"><a href="{{ url }}">{{ value }} »</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.
$pagination->html('links', array(
'wrapper' => '<ul class="pagination pagination-sm">{{ value }}</ul>',
));
Display pagination links.
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.
Display pager links. If you pass $previous and $next arrays, then there is no need to $this->set()
anything.
A prompt (string) for the previous page, or an array('url'=>'', 'title'=>'')
to pass the values directly.
A prompt (string) for the next page, or an array('url'=>'', 'title'=>'')
to pass the values directly.
Add the following to your composer.json
file.
{
"require ": {
"bootpress/pagination": "^1.0"
}
}
<?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>…</span></li>
<li><a href="http://example.com?page=10of10">10</a></li>
<li><a href="http://example.com?page=2of10">»</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 »</a></li>
</ul>
*/