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