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

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

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

設定

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:

パラメータ 規定値 説明
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.

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 two templates, the FuelPHP default, and a Twitter Bootstrap v2 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.
previous string
<span class=\"previous\">\n\t{link}\n</span>\n
Markup that will be used to generate the previous page markup.
previous-link string
\t\t<a href=\"{uri}\">{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}\">{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-link string
\t\t<a href=\"{uri}\">{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}\">{page}</a>\n
Markup that will be used to generate the next page link for inactive links.

In the template, {uri} will be replaced by the generated pagination link, 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
パラメータ
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.
返り値 Pagination
// 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()

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

Static No
Parameters None
返り値 string
// fetch a previously forged instance, and render it
echo Pagination::instance('mypagination')->render();

next($marker = '&raquo;')

The next method generates the markup to display a "Next" link for pagination.

Static No
パラメータ
パラメータ 規定値 説明
$marker
'&raquo;'
The text to be displayed in the link.
Returns string
Example
// fetch a previously forged instance, and render the "next page" link
echo Pagination::instance('mypagination')->next();

previous($marker = '&laquo;')

The previous method generates the markup to display a "Previous" link for pagination.

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

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
// 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
パラメータ
パラメータ 規定値 説明
$name Required The name of the property to fetch.
Returns mixed, properly value, or null if the property doesn't exist.
// 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
// 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::find()
	->order_by('date', 'ASC')
	->rows_offset(\Pagination::get('offset'))
	->rows_limit(\Pagination::get('per_page'))
	->get());