Pagination Class

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

How To

A simple example on how to use the Pagination class, the set_config method is used in this example. You can put this inside your action methods in your controller.

$config = array(
	'pagination_url' => 'http://localhost/fuel/welcome/index/',
	'total_items' => 10,
	'per_page' => 5,
	'uri_segment' => 3,
	'template' => array(
		'wrapper_start' => '<div class="my-pagination"> ',
		'wrapper_end' => ' </div>',
	),
);

// Config::set('pagination', $config); // you can use this too!
Pagination::set_config($config);

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

$data['pagination'] = Pagination::create_links();
$this->render('welcome/index', $data);

Configuration

You can configure the pagination using two methods either by using the set_config method or by setting a global config array.

The following configuration settings can be defined:

Param Type Default Description
pagination_url string None The URL of page where you have pagination.
uri_segment integer
3
The URI segment containing the page number.
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 1.
template array
array(...)
Associative array containing the HTML markup to create the pagination elements (see Global Config Array section for the list of array keys w/ default values).

Global Config Array

This method allows you to set the pagination configuration by setting a global config array.

Config::set('pagination', array(
	'pagination_url' => 'http://docs.fuelphp.com/',
	'uri_segment' => 2,
	'total_items' => 10,
	'per_page' => 20,
	'template' => array(
		'wrapper_start' => '<div class="pagination"> ',
		'wrapper_end' => ' </div>',
		'page_start' => '<span class="page-links"> ',
		'page_end' => ' </span>',
		'previous_start' => '<span class="previous"> ',
		'previous_end' => ' </span>',
		'previous_inactive_start' => ' </span class="previous-inactive">',
		'previous_inactive_end' => ' </span>',
		'previous_mark' => '« ',
		'next_start' => '<span class="next"> ',
		'next_end' => ' </span>',
		'next_inactive_start' => ' </span class="next-inactive">',
		'next_inactive_end' => ' </span>',
		'next_mark' => ' »',
		'active_start' => '<span class="active"> ',
		'active_end' => ' </span>',
		'regular_start' => '',
		'regular_end' => '',
	),
));

set_config(array $config)

The set_config method allows you to set the pagination configuration by passing an array.

Static Yes
Parameters
Param Default Description
$config Required The configuration array.
Returns void
Example
Pagination::set_config(array(
    'pagination_url' => 'http://docs.fuelphp.com/',
	'uri_segment' => 2,
	'total_items' => 10,
	'per_page' => 20,
	'template' => array(
		'wrapper_start' => '<div class="my-pagination-class"> ',
		'wrapper_end'   => ' </div>',
	),
));

The create_links method generates the pagination links in the view.

Static Yes
Parameters None
Returns mixed
Example
echo Pagination::create_links();

The next_link method displays a "Next" link for pagination.

Static Yes
Parameters
Param Default Description
$value Required The text displayed in the link.
Returns mixed
Example
echo Pagination::next_link('Next Page');

The prev_link method displays a "Previous" link for pagination.

Static Yes
Parameters
Param Default Description
$value Required The text displayed in the link.
Returns mixed
Example
echo Pagination::prev_link('Previous Page');

The page_links method displays the page links between previous and next links for pagination.

Static Yes
Parameters None
Returns mixed
Example
echo Pagination::page_links();