blob: 7d750bd23e751f6f0d32ab62328e200bbfaef346 [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
8If you are not familiar with the term "pagination", it refers to links
9that allows you to navigate from page to page, like this::
10
11 « First  < 1 2 3 4 5 >  Last »
12
13*******
14Example
15*******
16
17Here is a simple example showing how to create pagination in one of your
18:doc:`controller <../general/controllers>` functions::
19
Derek Jones36be9692011-10-05 15:52:41 -050020 $this->load->library('pagination');
21
22 $config['base_url'] = 'http://example.com/index.php/test/page/';
23 $config['total_rows'] = 200;
Marco Monteiro4b84d622012-06-26 11:53:20 +010024 $config['per_page'] = 20;
Derek Jones36be9692011-10-05 15:52:41 -050025
Marco Monteiro4b84d622012-06-26 11:53:20 +010026 $this->pagination->initialize($config);
Derek Jones36be9692011-10-05 15:52:41 -050027
28 echo $this->pagination->create_links();
Derek Jones8ede1a22011-10-05 13:34:52 -050029
30Notes
31=====
32
33The $config array contains your configuration variables. It is passed to
34the $this->pagination->initialize function as shown above. Although
35there are some twenty items you can configure, at minimum you need the
36three shown. Here is a description of what those items represent:
37
38- **base_url** This is the full URL to the controller class/function
39 containing your pagination. In the example above, it is pointing to a
40 controller called "Test" and a function called "page". Keep in mind
41 that you can :doc:`re-route your URI <../general/routing>` if you
42 need a different structure.
43- **total_rows** This number represents the total rows in the result
44 set you are creating pagination for. Typically this number will be
45 the total rows that your database query returned.
46- **per_page** The number of items you intend to show per page. In the
47 above example, you would be showing 20 items per page.
48
49The create_links() function returns an empty string when there is no
50pagination to show.
51
52Setting preferences in a config file
53====================================
54
55If you prefer not to set preferences using the above method, you can
56instead put them into a config file. Simply create a new file called
57pagination.php, add the $config array in that file. Then save the file
58in: config/pagination.php and it will be used automatically. You will
59NOT need to use the $this->pagination->initialize function if you save
60your preferences in a config file.
61
62**************************
63Customizing the Pagination
64**************************
65
66The following is a list of all the preferences you can pass to the
67initialization function to tailor the display.
68
69$config['uri_segment'] = 3;
70============================
71
72The pagination function automatically determines which segment of your
73URI contains the page number. If you need something different you can
74specify it.
75
76$config['num_links'] = 2;
77==========================
78
79The number of "digit" links you would like before and after the selected
80page number. For example, the number 2 will place two digits on either
81side, as in the example links at the very top of this page.
82
83$config['use_page_number'] = TRUE;
84==================================
85
86By default, the URI segment will use the starting index for the items
87you are paginating. If you prefer to show the the actual page number,
88set this to TRUE.
89
90$config['page_query_string'] = TRUE;
91====================================
92
93By default, the pagination library assume you are using :doc:`URI
94Segments <../general/urls>`, and constructs your links something
95like
96
97::
98
99 http://example.com/index.php/test/page/20
100
101
102If you have $config['enable_query_strings'] set to TRUE your links
103will automatically be re-written using Query Strings. This option can
104also be explictly set. Using $config['page_query_string'] set to TRUE,
105the pagination link will become.
106
107::
108
109 http://example.com/index.php?c=test&m=page&per_page=20
110
111
112Note that "per_page" is the default query string passed, however can be
113configured using $config['query_string_segment'] = 'your_string'
114
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100115$config['reuse_query_string'] = FALSE;
Derek Jonesce79be02012-06-25 23:23:46 -0700116======================================
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100117
Marco Monteiro4b84d622012-06-26 11:53:20 +0100118By default your Query String arguments (nothing to do with other
119query string options) will be ignored. Setting this config to
120TRUE will add existing query string arguments back into the
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100121URL after the URI segment and before the suffix
122
123::
124
125 http://example.com/index.php/test/page/20?query=search%term
126
127This helps you mix together normal :doc:`URI Segments <../general/urls>`
128as well as query string arguments, which until 3.0 was not possible.
129
Marco Monteiro4b84d622012-06-26 11:53:20 +0100130$config['prefix'] = '';
131==================================
132
133A custom prefix added to the path. The prefix value will be right before
134the offset segment.
135
136$config['suffix'] = '';
137==================================
138
139A custom suffix added to the path. The sufix value will be right after
140the offset segment.
141
Derek Jones8ede1a22011-10-05 13:34:52 -0500142***********************
143Adding Enclosing Markup
144***********************
145
146If you would like to surround the entire pagination with some markup you
147can do it with these two prefs:
148
149$config['full_tag_open'] = '<p>';
150===================================
151
152The opening tag placed on the left side of the entire result.
153
154$config['full_tag_close'] = '</p>';
155=====================================
156
157The closing tag placed on the right side of the entire result.
158
159**************************
160Customizing the First Link
161**************************
162
163$config['first_link'] = 'First';
164=================================
165
166The text you would like shown in the "first" link on the left. If you do
167not want this link rendered, you can set its value to FALSE.
168
169$config['first_tag_open'] = '<div>';
170======================================
171
172The opening tag for the "first" link.
173
174$config['first_tag_close'] = '</div>';
175========================================
176
177The closing tag for the "first" link.
178
179*************************
180Customizing the Last Link
181*************************
182
183$config['last_link'] = 'Last';
184===============================
185
186The text you would like shown in the "last" link on the right. If you do
187not want this link rendered, you can set its value to FALSE.
188
189$config['last_tag_open'] = '<div>';
190=====================================
191
192The opening tag for the "last" link.
193
194$config['last_tag_close'] = '</div>';
195=======================================
196
197The closing tag for the "last" link.
198
199***************************
200Customizing the "Next" Link
201***************************
202
203$config['next_link'] = '&gt;';
204===============================
205
206The text you would like shown in the "next" page link. If you do not
207want this link rendered, you can set its value to FALSE.
208
209$config['next_tag_open'] = '<div>';
210=====================================
211
212The opening tag for the "next" link.
213
214$config['next_tag_close'] = '</div>';
215=======================================
216
217The closing tag for the "next" link.
218
219*******************************
220Customizing the "Previous" Link
221*******************************
222
223$config['prev_link'] = '&lt;';
224===============================
225
226The text you would like shown in the "previous" page link. If you do not
227want this link rendered, you can set its value to FALSE.
228
229$config['prev_tag_open'] = '<div>';
230=====================================
231
232The opening tag for the "previous" link.
233
234$config['prev_tag_close'] = '</div>';
235=======================================
236
237The closing tag for the "previous" link.
238
239***********************************
240Customizing the "Current Page" Link
241***********************************
242
243$config['cur_tag_open'] = '<b>';
244==================================
245
246The opening tag for the "current" link.
247
248$config['cur_tag_close'] = '</b>';
249====================================
250
251The closing tag for the "current" link.
252
253****************************
254Customizing the "Digit" Link
255****************************
256
257$config['num_tag_open'] = '<div>';
258====================================
259
260The opening tag for the "digit" link.
261
262$config['num_tag_close'] = '</div>';
263======================================
264
265The closing tag for the "digit" link.
266
267****************
268Hiding the Pages
269****************
270
271If you wanted to not list the specific pages (for example, you only want
272"next" and "previous" links), you can suppress their rendering by
273adding::
274
275 $config['display_pages'] = FALSE;
276
Andrey Andreev88c47272012-06-17 02:32:31 +0300277****************************
278Adding attributes to anchors
279****************************
Derek Jones8ede1a22011-10-05 13:34:52 -0500280
Andrey Andreev88c47272012-06-17 02:32:31 +0300281If you want to add an extra attribute to be added to every link rendered
282by the pagination class, you can set them as key/value pairs in the
283"attributes" config
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300284
285::
286
Andrey Andreev88c47272012-06-17 02:32:31 +0300287 // Produces: class="myclass"
288 $config['attributes'] = array('class' => 'myclass');
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300289
Andrey Andreev88c47272012-06-17 02:32:31 +0300290.. note:: Usage of the old method of setting classes via "anchor_class"
291 is deprecated.
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300292
Andrey Andreev88c47272012-06-17 02:32:31 +0300293*****************************
294Disabling the "rel" attribute
295*****************************
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300296
Andrey Andreev88c47272012-06-17 02:32:31 +0300297By default the rel attribute is dynamically generated and appended to
298the appropriate anchors. If for some reason you want to turn it off,
299you can pass boolean FALSE as a regular attribute
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300300
Andrey Andreev88c47272012-06-17 02:32:31 +0300301::
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300302
Andrey Andreev88c47272012-06-17 02:32:31 +0300303 $config['attributes']['rel'] = FALSE;