blob: 15b3675dfbee4d704609205d61302fc448d48bed [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;
24 $config['per_page'] = 20;
25
26 $this->pagination->initialize($config);
27
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;
116====================================
117
118By 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
121URL 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
Derek Jones8ede1a22011-10-05 13:34:52 -0500130***********************
131Adding Enclosing Markup
132***********************
133
134If you would like to surround the entire pagination with some markup you
135can do it with these two prefs:
136
137$config['full_tag_open'] = '<p>';
138===================================
139
140The opening tag placed on the left side of the entire result.
141
142$config['full_tag_close'] = '</p>';
143=====================================
144
145The closing tag placed on the right side of the entire result.
146
147**************************
148Customizing the First Link
149**************************
150
151$config['first_link'] = 'First';
152=================================
153
154The text you would like shown in the "first" link on the left. If you do
155not want this link rendered, you can set its value to FALSE.
156
157$config['first_tag_open'] = '<div>';
158======================================
159
160The opening tag for the "first" link.
161
162$config['first_tag_close'] = '</div>';
163========================================
164
165The closing tag for the "first" link.
166
167*************************
168Customizing the Last Link
169*************************
170
171$config['last_link'] = 'Last';
172===============================
173
174The text you would like shown in the "last" link on the right. If you do
175not want this link rendered, you can set its value to FALSE.
176
177$config['last_tag_open'] = '<div>';
178=====================================
179
180The opening tag for the "last" link.
181
182$config['last_tag_close'] = '</div>';
183=======================================
184
185The closing tag for the "last" link.
186
187***************************
188Customizing the "Next" Link
189***************************
190
191$config['next_link'] = '&gt;';
192===============================
193
194The text you would like shown in the "next" page link. If you do not
195want this link rendered, you can set its value to FALSE.
196
197$config['next_tag_open'] = '<div>';
198=====================================
199
200The opening tag for the "next" link.
201
202$config['next_tag_close'] = '</div>';
203=======================================
204
205The closing tag for the "next" link.
206
207*******************************
208Customizing the "Previous" Link
209*******************************
210
211$config['prev_link'] = '&lt;';
212===============================
213
214The text you would like shown in the "previous" page link. If you do not
215want this link rendered, you can set its value to FALSE.
216
217$config['prev_tag_open'] = '<div>';
218=====================================
219
220The opening tag for the "previous" link.
221
222$config['prev_tag_close'] = '</div>';
223=======================================
224
225The closing tag for the "previous" link.
226
227***********************************
228Customizing the "Current Page" Link
229***********************************
230
231$config['cur_tag_open'] = '<b>';
232==================================
233
234The opening tag for the "current" link.
235
236$config['cur_tag_close'] = '</b>';
237====================================
238
239The closing tag for the "current" link.
240
241****************************
242Customizing the "Digit" Link
243****************************
244
245$config['num_tag_open'] = '<div>';
246====================================
247
248The opening tag for the "digit" link.
249
250$config['num_tag_close'] = '</div>';
251======================================
252
253The closing tag for the "digit" link.
254
255****************
256Hiding the Pages
257****************
258
259If you wanted to not list the specific pages (for example, you only want
260"next" and "previous" links), you can suppress their rendering by
261adding::
262
263 $config['display_pages'] = FALSE;
264
Andrey Andreev88c47272012-06-17 02:32:31 +0300265****************************
266Adding attributes to anchors
267****************************
Derek Jones8ede1a22011-10-05 13:34:52 -0500268
Andrey Andreev88c47272012-06-17 02:32:31 +0300269If you want to add an extra attribute to be added to every link rendered
270by the pagination class, you can set them as key/value pairs in the
271"attributes" config
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300272
273::
274
Andrey Andreev88c47272012-06-17 02:32:31 +0300275 // Produces: class="myclass"
276 $config['attributes'] = array('class' => 'myclass');
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300277
Andrey Andreev88c47272012-06-17 02:32:31 +0300278.. note:: Usage of the old method of setting classes via "anchor_class"
279 is deprecated.
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300280
Andrey Andreev88c47272012-06-17 02:32:31 +0300281*****************************
282Disabling the "rel" attribute
283*****************************
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300284
Andrey Andreev88c47272012-06-17 02:32:31 +0300285By default the rel attribute is dynamically generated and appended to
286the appropriate anchors. If for some reason you want to turn it off,
287you can pass boolean FALSE as a regular attribute
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300288
Andrey Andreev88c47272012-06-17 02:32:31 +0300289::
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300290
Andrey Andreev88c47272012-06-17 02:32:31 +0300291 $config['attributes']['rel'] = FALSE;