blob: fbc75ea56d6469ffaaf2281667655949ad98ac99 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001################
2Pagination Class
3################
4
5CodeIgniter's Pagination class is very easy to use, and it is 100%
6customizable, either dynamically or via stored preferences.
7
Andrey Andreevcc042092014-01-03 17:08:27 +02008.. contents::
9 :local:
10
11.. raw:: html
12
13 <div class="custom-index container"></div>
14
Derek Jones8ede1a22011-10-05 13:34:52 -050015If you are not familiar with the term "pagination", it refers to links
16that allows you to navigate from page to page, like this::
17
18 « First  < 1 2 3 4 5 >  Last »
19
20*******
21Example
22*******
23
24Here is a simple example showing how to create pagination in one of your
Andrey Andreevf4bee9b2014-01-03 13:19:49 +020025:doc:`controller <../general/controllers>` methods::
Derek Jones8ede1a22011-10-05 13:34:52 -050026
Derek Jones36be9692011-10-05 15:52:41 -050027 $this->load->library('pagination');
28
29 $config['base_url'] = 'http://example.com/index.php/test/page/';
30 $config['total_rows'] = 200;
Marco Monteiro4b84d622012-06-26 11:53:20 +010031 $config['per_page'] = 20;
Derek Jones36be9692011-10-05 15:52:41 -050032
Marco Monteiro4b84d622012-06-26 11:53:20 +010033 $this->pagination->initialize($config);
Derek Jones36be9692011-10-05 15:52:41 -050034
35 echo $this->pagination->create_links();
Derek Jones8ede1a22011-10-05 13:34:52 -050036
37Notes
38=====
39
Andrey Andreevf4bee9b2014-01-03 13:19:49 +020040The ``$config`` array contains your configuration variables. It is passed to
41the ``$this->pagination->initialize()`` method as shown above. Although
Derek Jones8ede1a22011-10-05 13:34:52 -050042there are some twenty items you can configure, at minimum you need the
43three shown. Here is a description of what those items represent:
44
45- **base_url** This is the full URL to the controller class/function
46 containing your pagination. In the example above, it is pointing to a
47 controller called "Test" and a function called "page". Keep in mind
48 that you can :doc:`re-route your URI <../general/routing>` if you
49 need a different structure.
50- **total_rows** This number represents the total rows in the result
51 set you are creating pagination for. Typically this number will be
52 the total rows that your database query returned.
53- **per_page** The number of items you intend to show per page. In the
54 above example, you would be showing 20 items per page.
55
Andrey Andreevf4bee9b2014-01-03 13:19:49 +020056The ``create_links()`` method returns an empty string when there is no
Derek Jones8ede1a22011-10-05 13:34:52 -050057pagination to show.
58
59Setting preferences in a config file
60====================================
61
62If you prefer not to set preferences using the above method, you can
63instead put them into a config file. Simply create a new file called
Andrey Andreevf4bee9b2014-01-03 13:19:49 +020064pagination.php, add the ``$config`` array in that file. Then save the file
65in *application/config/pagination.php* and it will be used automatically.
66You will NOT need to use ``$this->pagination->initialize()`` if you save
Derek Jones8ede1a22011-10-05 13:34:52 -050067your preferences in a config file.
68
69**************************
70Customizing the Pagination
71**************************
72
73The following is a list of all the preferences you can pass to the
74initialization function to tailor the display.
75
James L Parryc4a59572014-11-24 01:33:16 -080076**$config['uri_segment'] = 3;**
Derek Jones8ede1a22011-10-05 13:34:52 -050077
78The pagination function automatically determines which segment of your
79URI contains the page number. If you need something different you can
80specify it.
81
James L Parryc4a59572014-11-24 01:33:16 -080082**$config['num_links'] = 2;**
Derek Jones8ede1a22011-10-05 13:34:52 -050083
84The number of "digit" links you would like before and after the selected
85page number. For example, the number 2 will place two digits on either
86side, as in the example links at the very top of this page.
87
James L Parryc4a59572014-11-24 01:33:16 -080088**$config['use_page_numbers'] = TRUE;**
Derek Jones8ede1a22011-10-05 13:34:52 -050089
90By default, the URI segment will use the starting index for the items
91you are paginating. If you prefer to show the the actual page number,
92set this to TRUE.
93
James L Parryc4a59572014-11-24 01:33:16 -080094**$config['page_query_string'] = TRUE;**
Derek Jones8ede1a22011-10-05 13:34:52 -050095
96By default, the pagination library assume you are using :doc:`URI
97Segments <../general/urls>`, and constructs your links something
Andrey Andreevf4bee9b2014-01-03 13:19:49 +020098like::
Derek Jones8ede1a22011-10-05 13:34:52 -050099
100 http://example.com/index.php/test/page/20
101
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200102If you have ``$config['enable_query_strings']`` set to TRUE your links
Derek Jones8ede1a22011-10-05 13:34:52 -0500103will automatically be re-written using Query Strings. This option can
Andrey Andreev71d8f722017-01-17 12:01:00 +0200104also be explicitly set. Using ``$config['page_query_string']`` set to TRUE,
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200105the pagination link will become::
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
107 http://example.com/index.php?c=test&m=page&per_page=20
108
Derek Jones8ede1a22011-10-05 13:34:52 -0500109Note that "per_page" is the default query string passed, however can be
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200110configured using ``$config['query_string_segment'] = 'your_string'``
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
James L Parryc4a59572014-11-24 01:33:16 -0800112**$config['reuse_query_string'] = FALSE;**
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100113
Marco Monteiro4b84d622012-06-26 11:53:20 +0100114By default your Query String arguments (nothing to do with other
115query string options) will be ignored. Setting this config to
116TRUE will add existing query string arguments back into the
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200117URL after the URI segment and before the suffix.::
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100118
119 http://example.com/index.php/test/page/20?query=search%term
120
121This helps you mix together normal :doc:`URI Segments <../general/urls>`
122as well as query string arguments, which until 3.0 was not possible.
123
James L Parryc4a59572014-11-24 01:33:16 -0800124**$config['prefix'] = '';**
Marco Monteiro4b84d622012-06-26 11:53:20 +0100125
126A custom prefix added to the path. The prefix value will be right before
127the offset segment.
128
James L Parryc4a59572014-11-24 01:33:16 -0800129**$config['suffix'] = '';**
Marco Monteiro4b84d622012-06-26 11:53:20 +0100130
Andrey Andreev4c679b52017-10-04 12:12:26 +0300131A custom suffix added to the path. The suffix value will be right after
Marco Monteiro4b84d622012-06-26 11:53:20 +0100132the offset segment.
133
Andrey Andreev0da50122015-01-20 13:30:05 +0200134**$config['use_global_url_suffix'] = FALSE;**
135
136When set to TRUE, it will **override** the ``$config['suffix']`` value and
137instead set it to the one that you have in ``$config['url_suffix']`` in
138your **application/config/config.php** file.
139
Derek Jones8ede1a22011-10-05 13:34:52 -0500140***********************
141Adding Enclosing Markup
142***********************
143
144If you would like to surround the entire pagination with some markup you
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200145can do it with these two preferences:
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
James L Parryc4a59572014-11-24 01:33:16 -0800147**$config['full_tag_open'] = '<p>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500148
149The opening tag placed on the left side of the entire result.
150
James L Parryc4a59572014-11-24 01:33:16 -0800151**$config['full_tag_close'] = '</p>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
153The closing tag placed on the right side of the entire result.
154
155**************************
156Customizing the First Link
157**************************
158
James L Parryc4a59572014-11-24 01:33:16 -0800159**$config['first_link'] = 'First';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500160
161The text you would like shown in the "first" link on the left. If you do
162not want this link rendered, you can set its value to FALSE.
163
Andrey Andreevaef63e52014-02-13 14:49:55 +0200164.. note:: This value can also be translated via a language file.
165
James L Parryc4a59572014-11-24 01:33:16 -0800166**$config['first_tag_open'] = '<div>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
168The opening tag for the "first" link.
169
James L Parryc4a59572014-11-24 01:33:16 -0800170**$config['first_tag_close'] = '</div>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500171
172The closing tag for the "first" link.
173
James L Parryc4a59572014-11-24 01:33:16 -0800174**$config['first_url'] = '';**
Andrey Andreev8e814ee2014-04-04 13:21:07 +0300175
176An alternative URL to use for the "first page" link.
177
Derek Jones8ede1a22011-10-05 13:34:52 -0500178*************************
179Customizing the Last Link
180*************************
181
James L Parryc4a59572014-11-24 01:33:16 -0800182**$config['last_link'] = 'Last';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
184The text you would like shown in the "last" link on the right. If you do
185not want this link rendered, you can set its value to FALSE.
186
Andrey Andreevaef63e52014-02-13 14:49:55 +0200187.. note:: This value can also be translated via a language file.
188
James L Parryc4a59572014-11-24 01:33:16 -0800189**$config['last_tag_open'] = '<div>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500190
191The opening tag for the "last" link.
192
James L Parryc4a59572014-11-24 01:33:16 -0800193**$config['last_tag_close'] = '</div>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
195The closing tag for the "last" link.
196
197***************************
198Customizing the "Next" Link
199***************************
200
James L Parryc4a59572014-11-24 01:33:16 -0800201**$config['next_link'] = '&gt;';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
203The text you would like shown in the "next" page link. If you do not
204want this link rendered, you can set its value to FALSE.
205
Andrey Andreevaef63e52014-02-13 14:49:55 +0200206.. note:: This value can also be translated via a language file.
207
James L Parryc4a59572014-11-24 01:33:16 -0800208**$config['next_tag_open'] = '<div>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500209
210The opening tag for the "next" link.
211
James L Parryc4a59572014-11-24 01:33:16 -0800212**$config['next_tag_close'] = '</div>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
214The closing tag for the "next" link.
215
216*******************************
217Customizing the "Previous" Link
218*******************************
219
James L Parryc4a59572014-11-24 01:33:16 -0800220**$config['prev_link'] = '&lt;';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500221
222The text you would like shown in the "previous" page link. If you do not
223want this link rendered, you can set its value to FALSE.
224
Andrey Andreevaef63e52014-02-13 14:49:55 +0200225.. note:: This value can also be translated via a language file.
226
James L Parryc4a59572014-11-24 01:33:16 -0800227**$config['prev_tag_open'] = '<div>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500228
229The opening tag for the "previous" link.
230
James L Parryc4a59572014-11-24 01:33:16 -0800231**$config['prev_tag_close'] = '</div>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500232
233The closing tag for the "previous" link.
234
235***********************************
236Customizing the "Current Page" Link
237***********************************
238
James L Parryc4a59572014-11-24 01:33:16 -0800239**$config['cur_tag_open'] = '<b>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500240
241The opening tag for the "current" link.
242
James L Parryc4a59572014-11-24 01:33:16 -0800243**$config['cur_tag_close'] = '</b>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500244
245The closing tag for the "current" link.
246
247****************************
248Customizing the "Digit" Link
249****************************
250
James L Parryc4a59572014-11-24 01:33:16 -0800251**$config['num_tag_open'] = '<div>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500252
253The opening tag for the "digit" link.
254
James L Parryc4a59572014-11-24 01:33:16 -0800255**$config['num_tag_close'] = '</div>';**
Derek Jones8ede1a22011-10-05 13:34:52 -0500256
257The closing tag for the "digit" link.
258
259****************
260Hiding the Pages
261****************
262
263If you wanted to not list the specific pages (for example, you only want
264"next" and "previous" links), you can suppress their rendering by
265adding::
266
267 $config['display_pages'] = FALSE;
268
Andrey Andreev88c47272012-06-17 02:32:31 +0300269****************************
270Adding attributes to anchors
271****************************
Derek Jones8ede1a22011-10-05 13:34:52 -0500272
Andrey Andreev88c47272012-06-17 02:32:31 +0300273If you want to add an extra attribute to be added to every link rendered
274by the pagination class, you can set them as key/value pairs in the
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200275"attributes" config::
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300276
Andrey Andreev88c47272012-06-17 02:32:31 +0300277 // Produces: class="myclass"
278 $config['attributes'] = array('class' => 'myclass');
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300279
Andrey Andreev88c47272012-06-17 02:32:31 +0300280.. note:: Usage of the old method of setting classes via "anchor_class"
281 is deprecated.
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300282
Andrey Andreev88c47272012-06-17 02:32:31 +0300283*****************************
284Disabling the "rel" attribute
285*****************************
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300286
Andrey Andreev88c47272012-06-17 02:32:31 +0300287By default the rel attribute is dynamically generated and appended to
288the appropriate anchors. If for some reason you want to turn it off,
289you can pass boolean FALSE as a regular attribute
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300290
Andrey Andreev88c47272012-06-17 02:32:31 +0300291::
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300292
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200293 $config['attributes']['rel'] = FALSE;
294
295***************
296Class Reference
297***************
298
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200299.. php:class:: CI_Pagination
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200300
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200301 .. php:method:: initialize([$params = array()])
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200302
Andrey Andreev28c2c972014-02-08 04:27:48 +0200303 :param array $params: Configuration parameters
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200304 :returns: CI_Pagination instance (method chaining)
305 :rtype: CI_Pagination
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200306
307 Initializes the Pagination class with your preferred options.
308
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200309 .. php:method:: create_links()
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200310
Andrey Andreev28c2c972014-02-08 04:27:48 +0200311 :returns: HTML-formatted pagination
312 :rtype: string
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200313
Andrey Andreev4c679b52017-10-04 12:12:26 +0300314 Returns a "pagination" bar, containing the generated links or an empty string if there's just a single page.