Pagination クラス

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);

設定

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:

パラメータ 規定値 説明
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_inactive_attrs' => array(),
		'previous_mark'           => '« ',
		'previous_attrs'          => array(),
		'next_start'              => '<span class="next"> ',
		'next_end'                => ' </span>',
		'next_inactive_start'     => ' <span class="next-inactive">',
		'next_inactive_end'       => ' </span>',
		'next_inactive_attrs'     => array(),
		'next_mark'               => ' »',
		'next_attrs'              => array(),
		'active_start'            => '<span class="active"> ',
		'active_end'              => ' </span>',
		'active_attrs'            => array(),
		'regular_start'           => '',
		'regular_end'             => '',
		'regular_attrs'           => array(),
	),
));

If you have a standard configuration for your entire application, you can also opt to store this structure in the app/config/pagination.php config file, so you don't have to define it every time.

set_config(array $config)

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

Static Yes
パラメータ
パラメータ 規定値 説明
$config 必須 The configuration array.
返り値 void
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
パラメータ None
返り値 mixed
echo Pagination::create_links();

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

Static Yes
パラメータ
パラメータ 規定値 説明
$value 必須 The text displayed in the link.
返り値 mixed
echo Pagination::next_link('Next Page');

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

Static Yes
パラメータ
パラメータ 規定値 説明
$value 必須 The text displayed in the link.
返り値 mixed
echo Pagination::prev_link('Previous Page');

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

Static Yes
パラメータ None
返り値 mixed
echo Pagination::page_links();