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