blob: d8d31f8c5d8812a73e7bdda62f452dfd49672b8b [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
76$config['uri_segment'] = 3;
Andrey Andreevf4bee9b2014-01-03 13:19:49 +020077===========================
Derek Jones8ede1a22011-10-05 13:34:52 -050078
79The pagination function automatically determines which segment of your
80URI contains the page number. If you need something different you can
81specify it.
82
83$config['num_links'] = 2;
Andrey Andreevf4bee9b2014-01-03 13:19:49 +020084=========================
Derek Jones8ede1a22011-10-05 13:34:52 -050085
86The number of "digit" links you would like before and after the selected
87page number. For example, the number 2 will place two digits on either
88side, as in the example links at the very top of this page.
89
Bo-Yi Wu65bdaf52012-09-21 13:49:09 +080090$config['use_page_numbers'] = TRUE;
Andrey Andreev73766c22012-10-05 20:32:14 +030091===================================
Derek Jones8ede1a22011-10-05 13:34:52 -050092
93By default, the URI segment will use the starting index for the items
94you are paginating. If you prefer to show the the actual page number,
95set this to TRUE.
96
97$config['page_query_string'] = TRUE;
98====================================
99
100By default, the pagination library assume you are using :doc:`URI
101Segments <../general/urls>`, and constructs your links something
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200102like::
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
104 http://example.com/index.php/test/page/20
105
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200106If you have ``$config['enable_query_strings']`` set to TRUE your links
Derek Jones8ede1a22011-10-05 13:34:52 -0500107will automatically be re-written using Query Strings. This option can
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200108also be explictly set. Using ``$config['page_query_string']`` set to TRUE,
109the pagination link will become::
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
111 http://example.com/index.php?c=test&m=page&per_page=20
112
Derek Jones8ede1a22011-10-05 13:34:52 -0500113Note that "per_page" is the default query string passed, however can be
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200114configured using ``$config['query_string_segment'] = 'your_string'``
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100116$config['reuse_query_string'] = FALSE;
Derek Jonesce79be02012-06-25 23:23:46 -0700117======================================
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100118
Marco Monteiro4b84d622012-06-26 11:53:20 +0100119By default your Query String arguments (nothing to do with other
120query string options) will be ignored. Setting this config to
121TRUE will add existing query string arguments back into the
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200122URL after the URI segment and before the suffix.::
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100123
124 http://example.com/index.php/test/page/20?query=search%term
125
126This helps you mix together normal :doc:`URI Segments <../general/urls>`
127as well as query string arguments, which until 3.0 was not possible.
128
Marco Monteiro4b84d622012-06-26 11:53:20 +0100129$config['prefix'] = '';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200130=======================
Marco Monteiro4b84d622012-06-26 11:53:20 +0100131
132A custom prefix added to the path. The prefix value will be right before
133the offset segment.
134
135$config['suffix'] = '';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200136=======================
Marco Monteiro4b84d622012-06-26 11:53:20 +0100137
138A custom suffix added to the path. The sufix value will be right after
139the offset segment.
140
Derek Jones8ede1a22011-10-05 13:34:52 -0500141***********************
142Adding Enclosing Markup
143***********************
144
145If you would like to surround the entire pagination with some markup you
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200146can do it with these two preferences:
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
148$config['full_tag_open'] = '<p>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200149=================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
151The opening tag placed on the left side of the entire result.
152
153$config['full_tag_close'] = '</p>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200154===================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500155
156The closing tag placed on the right side of the entire result.
157
158**************************
159Customizing the First Link
160**************************
161
162$config['first_link'] = 'First';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200163================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
165The text you would like shown in the "first" link on the left. If you do
166not want this link rendered, you can set its value to FALSE.
167
Andrey Andreevaef63e52014-02-13 14:49:55 +0200168.. note:: This value can also be translated via a language file.
169
Derek Jones8ede1a22011-10-05 13:34:52 -0500170$config['first_tag_open'] = '<div>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200171====================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
173The opening tag for the "first" link.
174
175$config['first_tag_close'] = '</div>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200176======================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
178The closing tag for the "first" link.
179
180*************************
181Customizing the Last Link
182*************************
183
184$config['last_link'] = 'Last';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200185==============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500186
187The text you would like shown in the "last" link on the right. If you do
188not want this link rendered, you can set its value to FALSE.
189
Andrey Andreevaef63e52014-02-13 14:49:55 +0200190.. note:: This value can also be translated via a language file.
191
Derek Jones8ede1a22011-10-05 13:34:52 -0500192$config['last_tag_open'] = '<div>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200193===================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
195The opening tag for the "last" link.
196
197$config['last_tag_close'] = '</div>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200198=====================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500199
200The closing tag for the "last" link.
201
202***************************
203Customizing the "Next" Link
204***************************
205
206$config['next_link'] = '&gt;';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200207==============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500208
209The text you would like shown in the "next" page link. If you do not
210want this link rendered, you can set its value to FALSE.
211
Andrey Andreevaef63e52014-02-13 14:49:55 +0200212.. note:: This value can also be translated via a language file.
213
Derek Jones8ede1a22011-10-05 13:34:52 -0500214$config['next_tag_open'] = '<div>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200215===================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500216
217The opening tag for the "next" link.
218
219$config['next_tag_close'] = '</div>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200220=====================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500221
222The closing tag for the "next" link.
223
224*******************************
225Customizing the "Previous" Link
226*******************************
227
228$config['prev_link'] = '&lt;';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200229==============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500230
231The text you would like shown in the "previous" page link. If you do not
232want this link rendered, you can set its value to FALSE.
233
Andrey Andreevaef63e52014-02-13 14:49:55 +0200234.. note:: This value can also be translated via a language file.
235
Derek Jones8ede1a22011-10-05 13:34:52 -0500236$config['prev_tag_open'] = '<div>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200237===================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500238
239The opening tag for the "previous" link.
240
241$config['prev_tag_close'] = '</div>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200242=====================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500243
244The closing tag for the "previous" link.
245
246***********************************
247Customizing the "Current Page" Link
248***********************************
249
250$config['cur_tag_open'] = '<b>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200251================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500252
253The opening tag for the "current" link.
254
255$config['cur_tag_close'] = '</b>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200256==================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500257
258The closing tag for the "current" link.
259
260****************************
261Customizing the "Digit" Link
262****************************
263
264$config['num_tag_open'] = '<div>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200265==================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500266
267The opening tag for the "digit" link.
268
269$config['num_tag_close'] = '</div>';
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200270====================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500271
272The closing tag for the "digit" link.
273
274****************
275Hiding the Pages
276****************
277
278If you wanted to not list the specific pages (for example, you only want
279"next" and "previous" links), you can suppress their rendering by
280adding::
281
282 $config['display_pages'] = FALSE;
283
Andrey Andreev88c47272012-06-17 02:32:31 +0300284****************************
285Adding attributes to anchors
286****************************
Derek Jones8ede1a22011-10-05 13:34:52 -0500287
Andrey Andreev88c47272012-06-17 02:32:31 +0300288If you want to add an extra attribute to be added to every link rendered
289by the pagination class, you can set them as key/value pairs in the
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200290"attributes" config::
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300291
Andrey Andreev88c47272012-06-17 02:32:31 +0300292 // Produces: class="myclass"
293 $config['attributes'] = array('class' => 'myclass');
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300294
Andrey Andreev88c47272012-06-17 02:32:31 +0300295.. note:: Usage of the old method of setting classes via "anchor_class"
296 is deprecated.
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300297
Andrey Andreev88c47272012-06-17 02:32:31 +0300298*****************************
299Disabling the "rel" attribute
300*****************************
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300301
Andrey Andreev88c47272012-06-17 02:32:31 +0300302By default the rel attribute is dynamically generated and appended to
303the appropriate anchors. If for some reason you want to turn it off,
304you can pass boolean FALSE as a regular attribute
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300305
Andrey Andreev88c47272012-06-17 02:32:31 +0300306::
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300307
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200308 $config['attributes']['rel'] = FALSE;
309
310***************
311Class Reference
312***************
313
314.. class:: CI_Pagination
315
316 .. method:: initialize([$params = array()])
317
Andrey Andreev28c2c972014-02-08 04:27:48 +0200318 :param array $params: Configuration parameters
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200319 :returns: CI_Pagination instance (method chaining)
320 :rtype: CI_Pagination
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200321
322 Initializes the Pagination class with your preferred options.
323
324 .. method:: create_links()
325
Andrey Andreev28c2c972014-02-08 04:27:48 +0200326 :returns: HTML-formatted pagination
327 :rtype: string
Andrey Andreevf4bee9b2014-01-03 13:19:49 +0200328
329 Returns a "pagination" bar, containing the generated links or an empty string if there's just a single page.