Pagination Class

The pagination class allows you to easily setup pagination for records you display.

Simple examples on how to use the Pagination class . You can put this inside your action methods in your controller.

How To Paginate with the Query Builder

$config = array(
	'pagination_url' => 'http://localhost/fuel/welcome/index/',
	'total_items'    => 10,
	'per_page'       => 5,
	'uri_segment'    => 3,
	// or if you prefer pagination by query string
	//'uri_segment'    => 'page',
);

// Create a pagination instance named 'mypagination'
$pagination = Pagination::forge('mypagination', $config);

$data['example_data'] = DB::select('id', 'value')
							->from('pagination')
							->limit($pagination->per_page)
							->offset($pagination->offset)
							->execute()
							->as_array();

// we pass the object, it will be rendered when echo'd in the view
$data['pagination'] = $pagination;

// return the view
return \View::forge('welcome/index', $data);

How To Paginate with ORM

$config = array(
	'pagination_url' => 'http://localhost/fuel/posts/index/',
	'total_items'    => Model_Post::count(),
	'per_page'       => 10,
	'uri_segment'    => 3,
	// or if you prefer pagination by query string
	//'uri_segment'    => 'page',
);

$pagination = Pagination::forge('mypagination', $config);

$data['example_data'] = Model_Post::query()
							->rows_offset($pagination->offset)
							->rows_limit($pagination->per_page)
							->get();

// we pass the object, it will be rendered when echo'd in the view
$data['pagination'] = $pagination;

// return the view
return \View::forge('posts/index', $data);

Configuration

You can configure the pagination instance in several ways. You can pass an array with the configuration when you forge the instance, or you can update the properties directly on the instance.

The following configuration settings can be defined:

Param Type Default Description
pagination_url string
null
The URL of page where you have pagination. If null, Fuel tries to detect it from the current URL.
uri_segment integer|string
3
The URI segment containing the page number (if integer). The query string field containing the page number (if string).
num_links integer
5
The total number of links to show.
total_items integer
0
The total number of items. Usually this is the result of a count() query.
per_page integer
10
The number of items per page.
current_page integer
null
The page to load if no page number is present in the URI. If not given, it defaults to 'default_page'.
show_first bool
false
Generates a 'to the first page' link if true and not already on the first page.
show_last bool
false
Generates a 'to the last page' link if true and not already on the last page.
link_offset int / float
0.50
Offset of the active link in the pagination block, either in a decimal between 0 and 1, or in an integer (precentage) between 0 and 100.
default_page mixed
'first'
The page to load if no page number is present in the URI. Valid are 'first', 'last' or a page number. If not given, it defaults to 'first'.

Link offsets

By default, the Pagination class will try to position the active link (the one for the current page) in the middle of the pagination block.

You can control this behaviour using the link_offset value in the configuration. This value is defined either by a float between 0 and 1, or by an integer between 0 and 100 (like a percentage). By default this value is set to 0.5 (= 50%). If you make the value smaller, the active link will move to the left, if you make the value bigger, the active link will move to the right.

Example

Let's assume you have a total of 20 pages you can display. You have set num_links to 5, displaying links for 5 pages in total, and you're displaying previous and next links as well. The page you have currently selected is page 6.

  • With the default setting of 0.5,
    «  4  5  6  7  8  »
    is displayed, keeping the selected page 6 nicely in the middle of the block.
  • When you change the setting to 0, Pagination will always try to make the first link the active link. In this case,
    «  6  7  8  9  10  »
    is being displayed, making the selected page 6 the first link in the block.
  • If you would change the setting to 1, Pagination will always try to make the last link the active link. In this case,
    «  2  3  4  5  6  »
    will be displayed.

You can use any value in the range, to progressively shift the active focus either left or right. Obviously, the more links you display, the more granular you can shift the focus.

This only works if there are enough pages to do so. If you only have 5 pages available, you want to display 5 links, and page 3 is the current page, no previous and next links are displayed, and the current page will be in the middle, no matter what the setting of link_offset is. There are simply not enough pages available to move the current page link left or right.

Templating

Every pagination instance uses a template to generate the HTML needed to create the pagination markup. You can store your standard templates in the config/pagination.php file. Copy the file from the core config folder to your app config folder before you make any modifications. The default configuration file comes with three templates, the FuelPHP default, and a Twitter Bootstrap v2 and v3 compatible template.

The following template entries must be defined:

wrapper string
<div class="pagination">\n\t{pagination}\n</div>\n
Markup that will wrap the generated pagination markup.
first string
<span class="first">\n\t{link}\n</span>\n
Markup that will be used to generate the first page markup.
first-inactive string none Markup that will be used to generate the first page markup, if this page is first or single.
first-inactive-link string none Markup that will be used to generate the first page link, if this page is first or single.
first-marker string
&laquo;&laquo;
Markup that will be used to generate the marker of first page.
first-link string
\t\t<a href="{uri}">{page}</a>\n
Markup that will be used to generate the first page link.
previous string
<span class="previous">\n\t{link}\n</span>\n
Markup that will be used to generate the previous page markup.
previous-marker string
&laquo;
Markup that will be used to generate the marker of previous page.
previous-link string
\t\t<a href="{uri}" rel="prev">{page}</a>\n
Markup that will be used to generate the previous page link.
previous-inactive string
<span class="previous-inactive">\n\t{link}\n</span>\n
Markup that will be used to generate the previous page markup for inactive links.
previous-inactive-link string
\t\t<a href="{uri}" rel="prev">{page}</a>\n
Markup that will be used to generate the previous page markup for inactive links.
regular string
<span>\n\t{link}\n</span>\n
Markup that will be used to generate the markup for other pages.
regular-link string
\t\t<a href="{uri}">{page}</a>\n
Markup that will be used to generate the markup for other page links.
active string
<span class="active">\n\t{link}\n</span>\n
Markup that will be used to generate the markup for the current page.
active-link string
\t\t<a href="{uri}">{page}</a>\n
Markup that will be used to generate the markup for the link to the current page.
next string
<span class="next">\n\t{link}\n</span>\n
Markup that will be used to generate the next page markup.
next-marker string
&raquo;
Markup that will be used to generate the marker of next page.
next-link string
\t\t<a href="{uri}" rel="next">{page}</a>\n
Markup that will be used to generate the next page link.
next-inactive string
<span class="next-inactive">\n\t{link}\n</span>\n
Markup that will be used to generate the next page markup for inactive links.
next-inactive-link string
\t\t<a href="{uri}" rel="next">{page}</a>\n
Markup that will be used to generate the next page link for inactive links.
last string
<span class="last">\n\t{link}\n</span>\n
Markup that will be used to generate the last page markup.
last-marker string
&raquo;&raquo;
Markup that will be used to generate the marker of last page.
last-link string
\t\t<a href="{uri}">{page}</a>\n
Markup that will be used to generate the last page link.
last-inactive string none Markup that will be used to generate the last page markup, if this page is last or single.
last-inactive-link string none Markup that will be used to generate the last page link, if this page is last or single.

In the template, {uri} will be replaced by the generated pagination link (by # if inactive template), and {page} by the page number or the previous / next marker. If you want to use an image for these markers, just modify the corresponding link definitions in the template, replacing {page} by the markup for the image.

The configuration you pass when you forge the pagination instance will be merged with the default template defined in your configuration file. This will allow you to only pass the values you want to override. If your template in the configuration file is not complete, the default values as mentioned above will be used.

forge($name = 'default', $config = array())

The forge method allows you to create a new pagination instance, and configure it by passing an array.

Static Yes
Parameters
Param Default Description
$name Required The name of the instance to be created. If no name is given, the 'default' instance is created.
$config
array()
The configuration array.
Returns Pagination
Example
// create a new pagination instance
$pagination = Pagination::forge('mypagination', array(
	'pagination_url' => 'http://docs.fuelphp.com/',
	'uri_segment' => 2,
	'total_items' => 10,
	'per_page' => 20,
));

instance($name = null)

The instance method allows you retrieve a previously forged instance, or return the default instance if no name was given.

Static Yes
Parameters
Param Default Description
$name
null
The name of the instance to be returned. If no name is given, the 'default' instance is created.
Returns mixed. a Pagination object, or false if the requested instance does not exist.
Example
// fetch the previously forged instance
$pagination = Pagination::instance('mypagination');

render($raw = false)

The render method generates the markup to display the pagination links in the view.

Static No
Parameters
Param Default Description
$raw
false
If true, an array with raw pagination data is returned instead of the rendered pagination markup.
Returns string
Example
// fetch a previously forged instance, and render it
echo Pagination::instance('mypagination')->render();

The pagination object contains an __toString() method, that will cause the render() method to be called when you echo the object, or cast it to string.

first($marker = null)

The first method generates the markup to display a "First page" link for pagination. If no string is given to be used as marker, the "first-marker" value from the template will be used.

Static No
Parameters
Param Default Description
$marker
null
The text to be displayed in the link. Defaults to the 'first-marker' template value.
Returns string
Example
// fetch a previously forged instance, and render the "first page" link
echo Pagination::instance('mypagination')->first();

The "First page" link will only be shown if there is a first page, and you're not already on it. Note that by default there is no inactive link defined for 'first'.

previous($marker = null)

The previous method generates the markup to display a "Previous" link for pagination. If no string is given to be used as marker, the "previous-marker" value from the template will be used.

Static No
Parameters
Param Default Description
$marker
null
The text to be displayed in the link. Defaults to the 'previous-marker' template value.
Returns string
Example
// fetch a previously forged instance, and render the "previous page" link
echo Pagination::instance('mypagination')->previous();

next($marker = null)

The next method generates the markup to display a "Next" link for pagination. If no string is given to be used as marker, the "next-marker" value from the template will be used.

Static No
Parameters
Param Default Description
$marker
null
The text to be displayed in the link. Defaults to the 'next-marker' template value.
Returns string
Example
// fetch a previously forged instance, and render the "next page" link
echo Pagination::instance('mypagination')->next();

last($marker = null)

The last method generates the markup to display a "Last page" link for pagination. If no string is given to be used as marker, the "last-marker" value from the template will be used.

Static No
Parameters
Param Default Description
$marker
null
The text to be displayed in the link. Defaults to the 'last-marker' template value.
Returns string
Example
// fetch a previously forged instance, and render the "last page" link
echo Pagination::instance('mypagination')->last();

The "Last page" link will only be shown if there are any pages, and you're not already on the last one. Note that by default there is no inactive link defined for 'last'.

pages_render()

The pages_render method generates the markup that displays the page links between previous and next links for pagination.

Static No
Parameters None
Returns mixed
Example
// fetch a previously forged instance, and render the "pages" link
echo Pagination::instance('mypagination')->pages_render();

Static interface

For your convinience, the Pagination class also has a static interface that operates on the default instance only.

get($name)

The get method allows you to get a configuration item on the default instance.

Static Yes
Parameters
Param Default Description
$name Required The name of the property to fetch.
Returns mixed, properly value, or null if the property doesn't exist.
Example
// fetch the current page number
$page_number = Pagination::get('current_page');

// this is an alias of
$page_number = Pagination::instance()->current_page;

set($name, $value)

The set method allows you to set a configuration item on the default instance to the given value.

Static Yes
Parameters
Param Default Description
$name Required The name of the property to set.
$value Required The value to set the property to.
Returns void
Example
// set the total number of rows
$page_number = Pagination::set('total_items', 610);

// this is an alias of
Pagination::instance()->total_items = 610;

For backward compatibility, the static methods set_config(), create_links(), next_link() and prev_link() will be emulated on the default instance, minimizing the migration effort needed when upgrading an existing application to FuelPHP v1.4+.

Note that there is no possible way to emulate direct access to static class properties at the moment, so if your application uses that, you'll have to change them:

// pre v1.4 usage:
Pagination::$per_page = 10;

// new usage:
Pagination::set('per_page', 10);

// pre v1.4 usage:
Model_Article::find()
	->order_by('date', 'ASC')
	->rows_offset(\Pagination::$offset)
	->rows_limit(\Pagination::$per_page)
	->get();

// new usage:
Model_Article::query()
	->order_by('date', 'ASC')
	->rows_offset(\Pagination::get('offset'))
	->rows_limit(\Pagination::get('per_page'))
	->get();